[英]Make NA observations that are identical with observations in adjacent column
My data frame: 我的数据框:
structure(list(make = c("land rover", "land rover", "land rover",
"land rover", "land rover", "land rover", "land rover", "land rover",
"land rover", "land rover"), model = c("freelander", "range",
"discovery", "freelander", "range", "discovery", "discovery",
"range", "discovery", "range"), body = c("freelander", "sport",
"discovery", "2", "evoque", "sport", "4x4", "range", "4", "sport"
)), .Names = c("make", "model", "body"), row.names = c(92L, 178L,
185L, 192L, 202L, 224L, 233L, 448L, 473L, 480L), class = "data.frame")
I would like to NA those observations in column 'body' that are identical to observations in column 'model'. 我想在“身体”栏中的观察与在“模型”栏中的观察相同。 In this case this would be rows 1,3,8.
在这种情况下,这将是第1、3、8行。 I tried the following function which does not give any errors, but also does not produce any results:
我尝试了以下功能,该功能不会产生任何错误,但也不会产生任何结果:
RemovingNames <- function(df){
for(i in 1:nrow(df))
ifelse(identical (df[i,2], df[i,3]), df[i,3]<-NA, df[i,3]<-df[i,3])
}
如果将数据集称为“测试”,则:
test[test[,"model"] == test[, "body"], "body"] <- NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.