R确定一列中是否存在超过1个值,如果为真则变异连接

R determine if more than 1 value exists in a column, mutate concatenate if true

无法完全找到解决此问题的方法,正在寻找一些指导。

这是我的数据集示例:

     ID   Rank    Date       Date2.      Group   Group2
1   5678   1    2000-01-01   2010-05-02    A      A
2   5678   2    2010-05-02   2010-05-02    A      A
3   1234   1    2000-01-01   2015-06-03    B      A&B
4   1234   2    2015-06-03   2015-06-03    A      A&B

我想按 ID 分组并确定“组”列中是否存在该 ID 的多个值。如果是这样,将它们连接在一起。

Group2 是基于分组 ID 并查看组中是否存在多个值的所需输出。我正在使用 dplyr 但不确定从这里去哪里:

df <- df %>% group_by(ID) %>% mutate(Group2 = if else(?))

继续你的代码我会尝试:

df %>%
    group_by(ID) %>%
    mutate(Group2 = ifelse(n_distinct(Group) == 1, as.character(Group), paste0(sort(Group), collapse = "&")))

# A tibble: 4 x 6
# Groups:   ID [2]
     ID  Rank Date       Date2.     Group Group2
  <int> <int> <fct>      <fct>      <fct> <chr> 
1  5678     1 2000-01-01 2010-05-02 A     A     
2  5678     2 2010-05-02 2010-05-02 A     A     
3  1234     1 2000-01-01 2015-06-03 B     A&B   
4  1234     2 2015-06-03 2015-06-03 A     A&B   

我会建议下一个方法:

library(dplyr)
#Code
df %>% group_by(ID) %>%
  arrange(ID,Group) %>%
  #Identify unique elements
  mutate(NUnique=n_distinct(Group)) %>%
  mutate(Group2=ifelse(NUnique>1,paste0(Group,collapse = '&'),Group))

输出:

# A tibble: 4 x 7
# Groups:   ID [2]
     ID  Rank Date       Date2.     Group NUnique Group2
  <int> <int> <chr>      <chr>      <chr>   <int> <chr> 
1  1234     2 2015-06-03 2015-06-03 A           2 A&B   
2  1234     1 2000-01-01 2015-06-03 B           2 A&B   
3  5678     1 2000-01-01 2010-05-02 A           1 A     
4  5678     2 2010-05-02 2010-05-02 A           1 A  

使用了一些数据:

#Data
df <- structure(list(ID = c(5678L, 5678L, 1234L, 1234L), Rank = c(1L, 
2L, 1L, 2L), Date = c("2000-01-01", "2010-05-02", "2000-01-01", 
"2015-06-03"), Date2. = c("2010-05-02", "2010-05-02", "2015-06-03", 
"2015-06-03"), Group = c("A", "A", "B", "A")), row.names = c("1", 
"2", "3", "4"), class = "data.frame")

基本 R 选项

within(df,Group2 <- ave(Group,ID,FUN = function(x) ifelse(length(unique(x))==1,x,paste0(x,collapse = "&"))))

给予

    ID Rank       Date     Date2. Group Group2
1 5678    1 2000-01-01 2010-05-02     A      A
2 5678    2 2010-05-02 2010-05-02     A      A
3 1234    1 2000-01-01 2015-06-03     B    B&A
4 1234    2 2015-06-03 2015-06-03     A    B&A

我认为最简单的方法是 unique 函数。此函数创建一个向量,其中包含给定向量的不同值。

> df %>%
   group_by(ID)%>%
   mutate(Group2=paste(sort(unique(Group)),
                       collapse="&"))

# A tibble: 4 x 3
# Groups:   ID [2]
     ID Group Group2
  <dbl> <chr> <chr> 
1  5678 A     A     
2  5678 A     A     
3  1234 B     A&B   
4  1234 A     A&B   

选项data.table

library(data.table)
setDT(df)[, Group2 := paste(sort(unique(Group)), collapse="&"), ID]

数据

df <- structure(list(ID = c(5678L, 5678L, 1234L, 1234L), Rank = c(1L, 
2L, 1L, 2L), Date = c("2000-01-01", "2010-05-02", "2000-01-01", 
"2015-06-03"), Date2. = c("2010-05-02", "2010-05-02", "2015-06-03", 
"2015-06-03"), Group = c("A", "A", "B", "A")), row.names = c("1", 
"2", "3", "4"), class = "data.frame")