I have a large dataframe with a column of strings that have either 1 or 2 numbers at the end of the string. I'd like to extract these 1 or 2 numbers and place them in a new column. Here is how I am doing it using stringr
package and dplyr
:
library(tidyverse)
df <- structure(list(row = 1:9, Sensor = c("Inclin_01_A1", "Inclin_01_A2", "Inclin_01_A10", "Inclin_01_A25", "Inclin_01_B1", "Inclin_01_B2", "Inclin_01_B36", "Temp_F1", "Temp_F14")), row.names = c(NA, -9L), class = "data.frame")
df1 <- df %>%
mutate(newcol = if_else(str_sub(Sensor, -2, -2) == "A" | str_sub(Sensor, -2, -2) == "B" | str_sub(Sensor, -2, -2) =="F",
str_sub(Sensor, -1,-1),
str_sub(Sensor, -2)))
It seems clunky and takes a long time for my dataframe. Is there a less clunky and faster way to do this? Note that the character before the numbers will always be A
, B
, or F
.
NA
, one will return a potentially nonsense value that might blow up your analysis quietly.