Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

combining grep and a numerical comparison in r

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

your code gave the error because you add a extra , inside []. That is required when you try to subset on row indices. Here however we are trying to subset a vector. So no row/column indices

df$identifier[grepl("test", df$names) & df$age >=3] = 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...