使NA观测值与相邻列中的观测值相同

时间:2016-11-08 15:17:29

标签: r

我的数据框:

     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")

我想在列' body'这与列'模型'中的观察结果相同。在这种情况下,这将是行1,3,8。 我尝试了以下函数,它不会产生任何错误,但也不会产生任何结果:

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])
 }

1 个答案:

答案 0 :(得分:1)

如果您调用数据集&#34; test&#34;,则:

test[test[,"model"] == test[, "body"], "body"] <- NA