我想用Name.y中的值替换Name.x列的值,如果它不是NA(空行)
Name.x Name.y
US NA
Germany NA
Germany France
Canada NA
Italy Morocco
Austria Belgium
结果:
Name.x
US
Germany
France
Canada
Morocco
Belgium
答案 0 :(得分:1)
示例数据:
a <- data.frame("Name.x" = c("US", "Germany","Germany", "Canada", "Italy", "Austria"), "Name.y" = c(NA, NA, "France", NA, "Morocco", "Belgium"))
解决方案:
a$Name.x <- ifelse(is.na(a$Name.y), as.character(a$Name.x), as.character(a$Name.y))
答案 1 :(得分:0)
尝试这样的事情:
您的data.frame
db1<-data.frame(Name.x=c("US","Germany"),
Name.y=c(NA,"France"))
db1
Name.x Name.y
US <NA>
Germany France
要分析/替换的列名称
coldb1_NA<-"Name.y"
coldb_toSub<-"Name.x"
换人
db2<-db1
db2[,coldb_toSub]<-as.character(db1[,coldb_toSub])
db2[!is.na(db1[,coldb1_NA]),coldb_toSub]<-as.character(db1[!is.na(db1[,coldb1_NA]),coldb1_NA])
您的输出
db2
Name.x Name.y
1 US <NA>
2 France France