【发布时间】:2019-06-19 03:43:04
【问题描述】:
我对处理存储在列表中的数据帧还不是很熟悉。
我有一个整数列表,它们基本上表示两个数据帧的行的索引(df_nameA,df_nameB):
str(list1)
List of 2
$ df_nameA :int [1:3] 3 4 6
$ df_nameB :int [1:3] 1 2 4
df_nameA
nrow col1. col 3
1. a. A1
2. b. A2
3 c B1
4. d B2
5. e C1
6. f C2
df_nameB
nrow col1. col 3
1. g D1
2. h D2
3 i E1
4. l E2
5. m F1
6. n F2
list2<-list(df_nameA, df_nameB)
str(list2)
List of 2 : 6 observation and 3 variables
$:'dataframe'....
期望的输出:
df_nameA
nrow col1. col 3
3 c B1
4. d B2
6. f C2
df_nameB
nrow col1. col 3
1. g D1
2. h D2
4. l E2
基本上,我想根据列表 1 中存储的值对 list2 中的数据帧进行子集化。
我写了这样的东西,但它似乎不起作用:
for(i in seq_along(list1)){
for(i in seq_along(list2)){
lapply(list2, function(x) {return(x[x$nrow %in% list1[[i]],])})
}}
代码似乎从两个数据帧中对第 1、2、4 行进行了子集化 对我的代码有什么建议吗?
【问题讨论】:
标签: r