R:合并大数据框 [英] R: coalescing a large data frame
问题描述
假设我创建了一个数据框,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
解决方案,我可以在其中取出所需的列并将它们传递给 en大众.有什么想法吗?
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"
如何在无需编写的情况下合并
:
How do I coalesce
this without having to write:
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:合并大数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!