关于循环:基于另一个数据帧中的字符串,在 R 中的列表中子集多个数据帧 | 珊瑚贝

Subsetting multiple dataframes within list in R based on strings in another dataframe


我正在尝试根据包含在另一个数据帧中的字符串对列表中包含的多个数据帧进行子集化。

1
2
3
4
5
6
7
list.df <- list(
 df.1 = data.frame(LM = c(1:10), LS = c(1:10), PL = c(1:10)),
 df.2 = data.frame(XY = c(1:10), FE = c(4:13), OI = c(1:10)),
 df.3 = data.frame(IL = c(1:10), KU = c(9:18), TS = c(1:10)))

df.4 <- data.frame(df.1 = c(“LM”,”PL”, NA), df.2 = c(“FE”, NA, NA),
 df.3 = c(“IL”,”KU”,”TS”))

我希望我的所有数据框最终看起来像这样:

1
2
df.1_sub <- subset(list.df[[“df.1”]], select =
   colnames(list.df[[“df.1”]]) %in% df.4$df.1)

我将不得不对大约 50 个数据集执行此操作,并且想知道是否有一种方法可以编写一个循环来一次对所有数据集执行此操作。

我尝试过使用 lapply 和 for 循环,但到目前为止都没有成功。我是在 R 中使用列表的新手,不胜感激!
这是我第一次在堆栈溢出上发帖,如果我的帖子不合适,请告诉我,

  • 澄清一下,如果您创建了 df.2_sub 它只是 FE 列,对吗? df.3_sub 将是一个 10×3 的数据框,由列 IL、KU 和 TS 组成?
  • 对,那是正确的!


使用 Map 的一种方法是从 df.4 中删除 NA 值,并从 list.df

中子集相应的列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Map(function(x, y) x[as.character(na.omit(y))], list.df, df.4)

#$df.1
#   LM PL
#1   1  1
#2   2  2
#3   3  3
#4   4  4
#5   5  5
#6   6  6
#7   7  7
#8   8  8
#9   9  9
#10 10 10

#$df.2
#   FE
#1   4
#2   5
#3   6
#4   7
#5   8
#6   9
#7  10
#8  11
#9  12
#10 13

#$df.3
#   IL KU TS
#1   1  9  1
#2   2 10  2
#3   3 11  3
#…..

同样可以使用 purrr::map2

1
purrr::map2(list.df, df.4, ~.x[na.omit(as.character(.y))])

  • 对我来说太快了。 Map 而不是 mapply 可能更有意义,因为您并没有简化结果。
  • 可惜 df.4 有 factor 列,否则您可能会严重崩溃 – Map(`[`, list.df, lapply(df.4, na.omit)) – 不幸的是,目前给出了错误的答案。
  • 非常感谢您的回复!我已经尝试了上述方法,它在示例中运行良好,但是当我尝试为我的实际数据执行此操作时,我收到此错误 Error: Can’t find columns AD, AB, AW, AC, AL, … (and 32 more) in .数据`。我已经手动检查,这些列肯定在列表中的一个数据框中。有任何想法吗?
  • @Ricarda 这不适用于列表中的所有数据框。这将 df.4 的第一列与 list.df[[1]] 中的第一个列表进行子集化,df.4 的第二列将与 list.df[[2]] 子集化,依此类推。您是否要从整个 list.df 中对其进行子集化?
  • @Ronak,谢谢!我刚刚意识到 df.4 中列的顺序与 list.df 中 dfs 的顺序不同。基本上, df.4 中有一列对应于列表中的 dfs 之一。该列和数据框具有相同的名称。
  • @Ricarda 好的..在这种情况下,我们需要重新排序列表或数据框。尝试做 Map(function(x, y) x[as.character(na.omit(y))], list.df[names(df.4)], df.4)


我们可以使用 complete.cases 和 Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Map(function(x, y) x[complete.cases(y)], list.df, df.4)
#$df.1
#   LM LS
#1   1  1
#2   2  2
#3   3  3
#4   4  4
#5   5  5
#6   6  6
#7   7  7
#8   8  8
#9   9  9
#10 10 10

#$df.2
#   XY
#1   1
#2   2
#3   3
#4   4
#5   5
#6   6
#7   7
#8   8
#9   9
#10 10

#$df.3
#   IL KU TS
#1   1  9  1
#2   2 10  2
#3   3 11  3
#4   4 12  4
#5   5 13  5
#6   6 14  6
#7   7 15  7
#8   8 16  8
#9   9 17  9
#10 10 18 10

或使用 pmap

1
2
library(purrr)  
pmap(list(list.df, df.4), ~ .x[complete.cases(.y)])

来源:https://www.codenong.com/56469094/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9946.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?