I have a dataframe with a series ID that contains within it a state ID. I also have a vector containing the possible state IDs. Here is a sample:
df <- data.frame(seriesid = c("SMU01000002000000001", "SMU01000002000000001",
"SMU02000002000000001", "SMU04000002000000001",
"SMU04000002000000001", "SMU09000002000000001"))
stateid <- c(1:2, 4:6, 8:13)
stateid[stateid<10] <- paste0('0', stateid[stateid<10])
In the string, the two digits following "SMU" are the state IDs, so what I want to do is use the string paste0("SMU", stateid) to identify which state ID is contained within each series ID and then assign the state ID to a new variable. The final dataframe would look like
df <- data.frame(seriesid, stateid= c("01", "01", "02", "04", "04", "09"))
df
I am fairly new to R, and I realize that iterations of this question have been asked, but I haven't yet found or been able to adapt an answer that works.