我怎样才能在R上的相同的数据帧中结合行(基于重复值的特定列下)? [英] How can I combine rows within the same data frame in R (based on duplicate values under a specific column)?
问题描述
样品2(虚构的)的DF例如行:
用户ID facultyid courseid schoolid
167 265 1678 NA
167 71111 301 NA
假设我有几百重复的userid象在上面的例子。然而,绝大多数的用户标识有不同的值。
如何可以在这样的方式粘在列值中的行(2)的第1结合有重复的用户ID的行,除非该第一值是NA(在这种情况下,不适用下面将任何值来重新填充从第二行)?
在本质上,从上面的示例图,我的理想的输出将包含:
用户ID facultyid courseid schoolid
167 265 301 1678
合计(X = DF1,通过=列表(DF1 $用户ID),FUN =功能(X)na.omit (x)的[1])[ - 1]
或使用 dplyr
库:
库(dplyr)DF1%GT;%
GROUP_BY(用户ID)%GT;%
summarise_each(玩意儿(第一(na.omit(。))))
Sample of 2 (made-up) example rows in df:
userid facultyid courseid schoolid
167 265 NA 1678
167 71111 301 NA
Suppose that I have a couple hundred duplicate userid like in the above example. However, the vast majority of userid have different values.
How can I combine rows with duplicate userid in such a way as to stick to the column values in the 1st (of the 2) row unless the first value is NA (in which case the NA will be repopulated with whatever value came from the second row)?
In essence, drawing from the above example, my ideal output would contain:
userid facultyid courseid schoolid
167 265 301 1678
aggregate(x = df1, by = list(df1$userid), FUN = function(x) na.omit(x)[1])[,-1]
or use dplyr
library:
library(dplyr)
df1 %>%
group_by(userid) %>%
summarise_each(funs(first(na.omit(.))))
这篇关于我怎样才能在R上的相同的数据帧中结合行(基于重复值的特定列下)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!