定义一个列表dats有两个数据框,df1 and df2
dats <- list( df1 = data.frame(a=sample(1:3), b = sample(11:13)),
df2 = data.frame(a=sample(1:3), b = sample(11:13)))
> dats
$df1
a b
1 2 12
2 3 11
3 1 13
$df2
a b
1 3 13
2 2 11
3 1 12
我想删除变量a在每个数据框中。接下来我想添加一个变量,其中包含外部数据帧中每个数据帧的 id,例如:
ids <- data.frame(id=c("id1","id2"),df=c("df1","df2"))
> ids
id df
1 id1 df1
2 id2 df2
为了删除不必要的变量,我尝试了这个但没有运气:
> dats <- lapply(dats, function(x) assign(x, x[,c("b")]))
> Error in assign(x, x[, c("b")]) : invalid first argument
也不知道如何添加 id。
我也尝试过,也许更合适:
> temp <- lapply(dats, function(x) subset(x[1], select=x[[1]]$b))
Error in x[[1]]$b : $ operator is invalid for atomic vectors
我觉得令人困惑的是str(out[1])
返回一个列表,str(out[[1]])
返回一个数据帧。我认为这可能与此有关。