I have a dataframe with the following characteristics.
names<-c("test1", "test2", "test3", "not4", "test5")
age<-as.numeric(c(1,2,3,4,5))
identifier<-as.numeric(c(0,0,0,0,0))
df<-data.frame(names,age, identifier)
I am trying to set identifier to 1 for any row containing names like test and age >= 3 using the following code.
df$identifier[grep(".*test.*", df$names) & df$age>=3,]<-1
Error in df$identifier[grep(".*test.*", df$names) & df$age >= 3, ] :
incorrect number of dimensions
In addition: Warning message:
In grep(".*test.*", df$names) & df$age >= 3 :
longer object length is not a multiple of shorter object length
The following code does not seem to work either, with incorrect output (only test 3 and 5 should have been selected.)
df[grep(".*test.*", df$names) & df$age>=3,]
names age identifier
3 test3 3 0
4 not4 4 0
5 test5 5 0
Warning message:
In grep(".*test.*", df$names) & df$age >= 3 :
longer object length is not a multiple of shorter object length
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…