【发布时间】:2015-07-04 00:06:05
【问题描述】:
我有两个如下所示的数据框:
df1 <- data.frame(V1 = 1:4, V2 = rep(2, 4), V3 = 7:4)
df2 <- data.frame(V2 = rep(NA, 4), V1 = rep(NA, 4), V3 = rep(NA, 4))
如果两个数据框的列名相同,我需要编写一个函数,将 df1 的值分配给 df2。函数的结构应该是这样的:
fun <- function(x){
if(# If the name of x is the same like the name of a column in df1)
out <- df1$? # Here I need to assign df1$"x" somehow
out
}
fun(df2$V1)
输出应如下所示:
[1] 1 2 3 4
不幸的是,我自己找不到解决方案。有没有办法我可以做到这一点?非常感谢您!
【问题讨论】: