合并两行,其中一些在R中缺少值 [英] Merging two rows with some having missing values in R

查看:47
本文介绍了合并两行,其中一些在R中缺少值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问R社区如何合并具有相同ID(即相同参与者)的两个行,其中某些变量相同,而另一些变量存在NA.在我的示例中,我希望所有值4-5-6都显示在一行上,因此NA(或空单元格)消失了.

I would like to ask the R community how to merge two rows with the same ID (i.e. same participant) with some variables that are identical and others where there are NA's. In my example, I would like all the values 4-5-6 to appear on one row and therefore for the NA's (or empty cells) to be gone.

我尝试使用dplyr并没有取得很大的成功,而且我必须手动进行合并(这非常耗时,并且增加了出错的风险).预先感谢您对这个问题的帮助!

I have tried using dplyr without much success, and I have to do the merging by hand (which is quite time consuming and increases the risk for errors). Thank you in advance for your help with this problem!

推荐答案

# Create sample data frame.
id <- c(rep('Participant 1', 2), rep('Participant 2', 2))
value1 <- rep('A', 4)
value2 <- rep('B', 4)
value3 <- rep('C', 4)
value4 <- c('x', NA, NA, 'x')
value5 <- c('x', NA, 'x', NA)
value6 <- c(NA, 'x', NA, 'x')

df <- data.frame(id, value1, value2, value3, value4, value5, value6, stringsAsFactors = F)

# Use dplyr to group the data and keep the non-NA value from the other columns.
df %>% group_by(id, value1, value2, value3) %>%
       summarise(value4 = max(value4, na.rm = T),
                 value5 = max(value5, na.rm = T),
                 value6 = max(value6, na.rm = T))

这篇关于合并两行,其中一些在R中缺少值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆