使用列表中的数据框:删除变量,添加新变量 [英] Working with dataframes in a list: Drop variables, add new ones
问题描述
使用两个数据框 df1 和 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的变量,例如:
I would like to drop variable a in each data frame. Next I would like to add a variable with the id of each dataframe from an external dataframe, like:
ids <- data.frame(id=c("id1","id2"),df=c("df1","df2"))
> ids
id df
1 id1 df1
2 id2 df2
要删除不必要的var,我没有运气尝试过此操作:
To drop unnecessary vars I tried this without luck:
> dats <- lapply(dats, function(x) assign(x, x[,c("b")]))
> Error in assign(x, x[, c("b")]) : invalid first argument
也不知道如何添加ID.
Not sure how to add the id either.
我也尝试过,也许更合适:
I also tried, perhaps more appropriately:
> 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]])
返回一个数据帧.我认为这可能与它有关.
What I find confusing is that str(out[1])
returns a list, str(out[[1]])
returns a dataframe. I think that may have something to do with it.
推荐答案
或尝试以下操作:将ids
提取到命名向量中,该向量将数据帧名称映射到id:
Or try this: Extract your ids
into a named vector that maps the data-frame name to the id:
df2id <- ids$id
names(df2id) <- ids$df
> df2id
df1 df2
id1 id2
Levels: id1 id2
然后使用mapply
两者(a)从每个数据帧中删除a
列,以及(b)添加id
列:
Then use mapply
to both (a) drop the a
column from each data-frame, and (b) add the id
column:
> mapply( function(d,x) cbind( subset(d, select = -a),
+ id = x),
+ dats, df2id[ names(dats) ] ,
+ SIMPLIFY=FALSE)
$df1
b id
1 12 id1
2 11 id1
3 13 id1
$df2
b id
1 12 id2
2 11 id2
3 13 id2
请注意,我们将df2id[ names(dats) ]
传递给mapply
-这确保了df2id
中的数据帧与dats
中的数据帧对齐".
Note that we are passing df2id[ names(dats) ]
to the mapply
-- this ensures that the data-frames in df2id
are "aligned" with the data-frames in dats
.
这篇关于使用列表中的数据框:删除变量,添加新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!