根据另一个数据集的元素向数据集添加多个新列

时间:2020-10-28 21:29:27

标签: r

我有以下产品列表

> products
# A tibble: 311 x 1
   value                    
   <fct>                    
 1 NA                       
 2 Alternativ Economy
 3 Ambulant Balance  
 4 Ambulant Economy  
 5 Ambulant Premium  
 6 Ambulant 2               
 7 Ambulant 3               
 8 Ambulant 1               
 9 COMPLETA                 
10 HOSPITAL ECO             
# ... with 301 more rows

和以下df

> df <- data.frame(employee = c('John Doe','Peter Gynn','Jolie Hope'), 
+           salary = c(21000, 23400, 26800), 
+           startdate = as.Date(c('2010-11-1','2008-3-25','2007-3-14')))
> df
    employee salary  startdate
1   John Doe  21000 2010-11-01
2 Peter Gynn  23400 2008-03-25
3 Jolie Hope  26800 2007-03-14

现在,我想将前者(即产品)的元素添加为后者(即df)的变量。我用

cbind(df, setNames(lapply(products, function(x) x = NA), products))

但是我得到一个错误。您能建议另一种方法吗?我的解决方案出了什么问题?预先感谢

1 个答案:

答案 0 :(得分:0)

这是一种解决方法。

df <- data.frame(employee = c('John Doe','Peter Gynn','Jolie Hope'), 
                 salary = c(21000, 23400, 26800), 
                 startdate = as.Date(c('2010-11-1','2008-3-25','2007-3-14')))

products <- data.frame(value = c(NA, "Alternativ Economy", "COMPLETA"))
#products$value <- ifelse(is.na(products$value), "not_available", as.character(products$value))

cbind(df, `colnames<-`(data.frame(matrix(ncol = nrow(products), nrow = nrow(df))), products$value))

    employee salary  startdate NA Alternativ Economy COMPLETA
1   John Doe  21000 2010-11-01 NA                 NA       NA
2 Peter Gynn  23400 2008-03-25 NA                 NA       NA
3 Jolie Hope  26800 2007-03-14 NA                 NA       NA

我质疑将NA作为列名的明智性,因此我不建议在其中的一行代码将NA替换为某些字符串。