首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于2列中的相似性折叠R中的行

基于2列中的相似性折叠R中的行
EN

Stack Overflow用户
提问于 2018-02-06 03:06:50
回答 3查看 95关注 0票数 0

我对R比较陌生,在根据多个列中的相似性合并行时遇到了麻烦。我有以下数据集

代码语言:javascript
复制
LAST_NAME   FIRST_NAME  INTERVAL    VISIT_DATE  MFQ_1   MFQ_2   MFQ_3   Handedness  ARI_1   ARI_2   ARI_4   ARI_COMPLETED_BY
Doe Jane    Interval 1  1/1/99  4   6   2   Na  Na  Na  Na  Na
Doe Jane    Interval 1  1/1/99  Na  Na  Na  Right-Handed    Na  Na  Na  Na
Doe Jane    Interval 1  1/1/99  Na  Na  Na  Na  4   2   2   Dad
Doe Jane    Interval 2  2/4/04  Na  Na  Na  Right-Handed    Na  Na  Na  Na
Doe Jane    Interval 2  2/4/04  5   6   3   Na  Na  Na  Na  Na 
Doe Jane    Interval 2  2/4/04  Na  Na  Na  Na  4   5   5   Mom
Smith   Joe Interval 1  3/1/01  5   1   7   Na  Na  Na  Na  Na
Smith   Joe Interval 1  3/1/01  Na  Na  Na  Left-Handed Na  Na  Na  Na
Smith   Joe Interval 1  3/1/01  Na  Na  Na  Na  8   8   2   Dad
Smith   Joe Interval 2  5/4/09  Na  Na  Na  Na  8   5   4   Dad
Smith   Joe Interval 2  5/4/09  7   2   8   Na  Na  Na  Na  Na
Smith   Joe Interval 2  5/4/09  Na  Na  Na  Left-Handed Na  Na  Na  Na

我想根据名称/间隔/日期合并行,这样看起来就像这样:

代码语言:javascript
复制
LAST_NAME   FIRST_NAME  INTERVAL    VISIT_DATE  MFQ_1   MFQ_2   MFQ_3   Handedness  ARI_1   ARI_2   ARI_4   ARI_COMPLETED_BY
Doe Jane    Interval 1  1/1/99  4   6   2   Right-Handed    4   2   2   Dad
Doe Jane    Interval 2  2/4/04  5   6   3   Right-Handed    4   5   5   Mom
Smith   Joe Interval 1  3/1/01  5   1   7   Left-Handed 8   8   2   Dad
Smith   Joe Interval 2  5/4/09  7   2   8   Left-Handed 8   5   4   Dad

我已经尝试了以下代码:

代码语言:javascript
复制
CTDB %>% group_by(LAST_NAME:VISIT_DATE) %>% summarise_all(funs(na.omit(.)))

但是我得到了以下错误

代码语言:javascript
复制
Error in mutate_impl(.data, dots) : Evaluation error: NA/NaN argument.
In addition: Warning messages:
1: In LAST_NAME:VISIT_DATE :
  numerical expression has 3326 elements: only the first used
2: In LAST_NAME:VISIT_DATE :
  numerical expression has 3326 elements: only the first used
3: In evalq(LAST_NAME:VISIT_DATE, <environment>) :
  NAs introduced by coercion
4: In evalq(LAST_NAME:VISIT_DATE, <environment>) :
  NAs introduced by coercion

我不确定如何解决这个问题才能得到想要的结果。任何帮助都将不胜感激!

EN

回答 3

Stack Overflow用户

发布于 2018-02-06 03:40:13

您可以在vars(...)中使用group_by_at。(请注意,na.omit并不做您认为它做的事情。na.exclude更接近您想要的效果)。如果您的值实际上是NA,那么您可以改用i[!is.na(i)]

代码语言:javascript
复制
library(tidyverse)
df %>% 
  group_by_at(vars(LAST_NAME:VISIT_DATE)) %>% 
  summarise_all(function(i) { i[i!="Na"] })

df <- read.table(text="LAST_NAME   FIRST_NAME  INTERVAL    VISIT_DATE  MFQ_1   MFQ_2   MFQ_3   Handedness  ARI_1   ARI_2   ARI_4   ARI_COMPLETED_BY
Doe Jane    Interval_1  1/1/99  4   6   2   Na  Na  Na  Na  Na
Doe Jane    Interval_1  1/1/99  Na  Na  Na  Right-Handed    Na  Na  Na  Na
Doe Jane    Interval_1  1/1/99  Na  Na  Na  Na  4   2   2   Dad
Doe Jane    Interval_2  2/4/04  Na  Na  Na  Right-Handed    Na  Na  Na  Na
Doe Jane    Interval_2  2/4/04  5   6   3   Na  Na  Na  Na  Na 
Doe Jane    Interval_2  2/4/04  Na  Na  Na  Na  4   5   5   Mom
Smith   Joe Interval_1  3/1/01  5   1   7   Na  Na  Na  Na  Na
Smith   Joe Interval_1  3/1/01  Na  Na  Na  Left-Handed Na  Na  Na  Na
Smith   Joe Interval_1  3/1/01  Na  Na  Na  Na  8   8   2   Dad
Smith   Joe Interval_2  5/4/09  Na  Na  Na  Na  8   5   4   Dad
Smith   Joe Interval_2  5/4/09  7   2   8   Na  Na  Na  Na  Na
Smith   Joe Interval_2  5/4/09  Na  Na  Na  Left-Handed Na  Na  Na  Na", header=TRUE, stringsAsFactors=FALSE)
票数 1
EN

Stack Overflow用户

发布于 2018-02-06 05:35:59

首先,您需要用显式的NA值替换"Na“字符串

代码语言:javascript
复制
CTDB[CTDB == "Na"] <- NA

您也不能在grouping函数中使用:,因此我们将列出我们想要分组的列。然后用first()包装na.omit(),因为na.omit本身并不是一个聚合函数,它也不会告诉dplyr如何汇总。

代码语言:javascript
复制
CTDB %>% group_by(LAST_NAME, FIRST_NAME, INTERVAL, VISIT_DATE) %>% 
  summarize_all(funs(first(na.omit(.))))
票数 0
EN

Stack Overflow用户

发布于 2018-02-06 06:56:02

使用基数R:

代码语言:javascript
复制
df[df=="Na]=NA
aggregate(df,df[1:4],na.omit)[-(5:8)]
  LAST_NAME FIRST_NAME   INTERVAL VISIT_DATE MFQ_1 MFQ_2 MFQ_3   Handedness ARI_1 ARI_2 ARI_4 ARI_COMPLETED_BY
1       Doe       Jane Interval_1     1/1/99     4     6     2 Right-Handed     4     2     2              Dad
2       Doe       Jane Interval_2     2/4/04     5     6     3 Right-Handed     4     5     5              Mom
3     Smith        Joe Interval_1     3/1/01     5     1     7  Left-Handed     8     8     2              Dad
4     Smith        Joe Interval_2     5/4/09     7     2     8  Left-Handed     8     5     4              Dad
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48629735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档