根据R中另一列的值设置一列的值

时间:2019-09-24 08:17:43

标签: r dataframe match

我用来自另一个数据框的值创建了一个新列。问题在于两个数据帧中都有空行。因此,我为第一个表中的空行分配了错误的名称,第一个空行与第二个表。

我的代码:

df1 <- data.frame(age=c(23," ",55,34,45),
                  name=c("A","S","P","J","M"))


df2 <- data.frame(age=c(" ",43,55,34,45),
                  name=c("Alex","Silvia","Peter","Jack","Michael"))


df1$names2 <- df2$name[match(df1$age, df2$age)]

> df1
  age name  names2
1  23    A    <NA>
2        S    Alex
3  55    P   Peter
4  34    J    Jack
5  45    M Michael

第一个结果记录可以,但是第二个显示错误的信息。

1 个答案:

答案 0 :(得分:0)

这可以通过多种方式解决。快速解决方案是将match中的names2中相应的age空值替换为NA之后。

df1$names2 <- df2$name[match(df1$age, df2$age)]
df1$names2[df1$age == " "] <- NA

df1
#  age name  names2
#1  23    A    <NA>
#2        S    <NA>
#3  55    P   Peter
#4  34    J    Jack
#5  45    M Michael

或者在执行match之前,删除空值

inds <- df1$age != " "
df1$names2[inds] <- df2$name[match(df1$age[inds], df2$age)]

数据

df1 <- data.frame(age=c(23," ",55,34,45),
          name=c("A","S","P","J","M"), stringsAsFactors = FALSE)

df2 <- data.frame(age=c(" ",43,55,34,45),
        name=c("Alex","Silvia","Peter","Jack","Michael"), stringsAsFactors = FALSE)