问题描述
假设我创建一个数据框, foo
:
Say I create a data frame, foo
:
foo <- data.frame(A=rep(NA,10),B=rep(NA,10))
foo$A[1:3] <- "A"
foo$B[6:10] <- "B"
看起来像
A B
1 A <NA>
2 A <NA>
3 A <NA>
4 <NA> <NA>
5 <NA> <NA>
6 <NA> B
7 <NA> B
8 <NA> B
9 <NA> B
10 <NA> B
我可以将此代码 coalesce
合并为一列,例如:
I can coalesce
this into a single column, like this:
data.frame(AB = coalesce(foo$A, foo$B))
给予
AB
1 A
2 A
3 A
4 <NA>
5 <NA>
6 B
7 B
8 B
9 B
10 B
这很好.现在,说我的数据框很大,有很多列.如何 coalesce
,而不必分别命名每个列?据我了解, coalesce
是期望的向量,所以我看不到一个整洁的 dplyr
解决方案,在该解决方案中,我只能选择所需的列并将其传递给群众.有什么想法吗?
which is nice. Now, say my data frame is huge with lots of columns. How do I coalesce
that without naming each column individually? As far as I understand, coalesce
is expecting vectors, so I don't see a neat and tidy dplyr
solution where I can just pluck out the required columns and pass them en masse. Any ideas?
编辑
根据要求,提供一个较难"的示例.
As requested, a "harder" example.
foo <- data.frame(A=rep(NA,10),B=rep(NA,10),C=rep(NA,10),D=rep(NA,10),E=rep(NA,10),F=rep(NA,10),G=rep(NA,10),H=rep(NA,10),I=rep(NA,10),J=rep(NA,10))
foo$A[1] <- "A"
foo$B[2] <- "B"
foo$C[3] <- "C"
foo$D[4] <- "D"
foo$E[5] <- "E"
foo$F[6] <- "F"
foo$G[7] <- "G"
foo$H[8] <- "H"
foo$I[9] <- "I"
foo$J[10] <- "J"
我如何无需编写而 coalesce
:
data.frame(ALL= coalesce(foo$A, foo$B, foo$C, foo$D, foo$E, foo$F, foo$G, foo$H, foo$I, foo$J))
推荐答案
您可以使用 do.call(coalesce,...)
,这是一种使用很多争论:
You can use do.call(coalesce, ...)
, which is a simpler way to write a function call with a lot of arguments:
library(dplyr)
do.call(coalesce, foo)
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
这篇关于R:合并一个大数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!