Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...