基于另一列中的共同值,如何复制行?

3
我有一个数据框,其中包括一列具有个人ID(唯一)的数据,和第二列显示一个共同的唯一变量。也就是说,第一列中的每个人都采取了在B列中显示的相同行动。
我想在R中编写代码,创建新的行,根据B列将A列的所有人匹配成对。
也就是说,给定以下示例:
person <- c("a", "b", "c", "d", "e", "f") 
action <- c("x", "x", "x", "y", "y", "y") 
data.frame(person, action)

我想创建这个:

person1 <- c("a", "a", "b", "d", "d", "e") 
person2 <- c("b", "c", "c", "e", "f", "f")
data.frame(person1, person2)
4个回答

3
一种使用group_modify()combn()的方法:
library(dplyr)

df %>%
  group_by(action) %>%
  group_modify(~ as_tibble(t(combn(pull(.x, person), 2))))

# A tibble: 6 × 3
# Groups:   action [2]
  action V1    V2   
  <chr>  <chr> <chr>
1 x      a     b    
2 x      a     c    
3 x      b     c    
4 y      d     e    
5 y      d     f    
6 y      e     f   

1
这个怎么样?
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
#> Warning: package 'tidyr' was built under R version 4.1.2
person<-c("a", "b", "c", "d", "e", "f") 
action<-c("x", "x", "x", "y", "y", "y") 
dat <- data.frame(person, action)

dat %>% 
  group_by(action) %>% 
  summarise(person = as.data.frame(t(combn(person, 2)))) %>% 
  unnest(person) %>% 
  rename(person1=V1, person2=V2)
#> `summarise()` has grouped output by 'action'. You can override using the
#> `.groups` argument.
#> # A tibble: 6 × 3
#> # Groups:   action [2]
#>   action person1 person2
#>   <chr>  <chr>   <chr>  
#> 1 x      a       b      
#> 2 x      a       c      
#> 3 x      b       c      
#> 4 y      d       e      
#> 5 y      d       f      
#> 6 y      e       f

此内容是由 reprex package (v2.0.1) 于2022年4月21日创建


1

以下是基于R语言的一行代码。

person <- c("a", "b", "c", "d", "e", "f") 
action <- c("x", "x", "x", "y", "y", "y") 
df <- data.frame(person, action)

setNames(
  do.call(
    rbind,
    lapply(split(df, df$action),
           function(x) as.data.frame(t(combn(x$person, 2))))),
  c("person1", "person2"))

#     person1 person2
# x.1       a       b
# x.2       a       c
# x.3       b       c
# y.1       d       e
# y.2       d       f
# y.3       e       f

0

使用 base R

subset(merge(dat, dat, by = 'action'), person.x != person.y & 
  duplicated(paste(pmin(person.x, person.y), pmax(person.x, person.y))))
   action person.x person.y
4       x        b        a
7       x        c        a
8       x        c        b
13      y        e        d
16      y        f        d
17      y        f        e

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接