R中的交叉复制

luaexgnf  于 2023-02-27  发布在  其他
关注(0)|答案(3)|浏览(115)

我需要一些帮助来解决以下问题。我有一个数据集,其组成如下

from   to
A      B
A      B
C      D
C      D

我想获取以下数据集

from   to
A      B
A      C
C      A
C      D

基本上,在group_by(from)之后,我希望在“to”列中的值之间、第一组中“from”列的最后一个值和第二组中“from”列的第一个值之间进行“交叉复制”,以此类推,我使用了“complete”,但它没有帮助。

qco9c6ql

qco9c6ql1#

获取您想要更改的值的索引(无需分组),并将其替换为rev ersed值:

library(dplyr)
idx <- which(lag(df$from) != df$from | lead(df$from) != df$from)
df[idx, "to"] <- df$from[c(matrix(idx, nrow = 2)[2:1, ])]

输出

from to
1    A  B
2    A  C
3    C  A
4    C  D
odopli94

odopli942#

使用base R

lst1 <- split(df1$to, df1$from)
df1$to <- unlist(Map(\(x, nm, i) {x[i] <- nm; x},
     lst1, rev(names(lst1)), length(lst1):1))
  • 输出
df1
 from to
1    A  B
2    A  C
3    C  A
4    C  D
66bbxpm5

66bbxpm53#

下面是一个tidyverse解决方案:

library(tidyverse) 

dat <- tibble(
  from = c("A", "A", "C", "C"), 
  to = c("B", "B", "D", "D")
)
sol <- dat %>% 
  mutate(
    fst = lag(from), # lag `from` for first values
    lst = lead(from) # lead `from` for last values
  ) %>% 
  group_by(from) %>% 
  transmute(
    to = case_when( 
      row_number() == 1 & !is.na(fst) ~ fst, # if first element in group and lagged `from` is not NA then equals lagged `from`
      row_number() == n() & !is.na(lst) ~ lst, # if last element in group and leaded `from` is not NA, then equals leaded `from`
      T ~ to # else `to`
    )
  ) %>% 
  ungroup()

sol
#> # A tibble: 4 × 2
#>   from  to   
#>   <chr> <chr>
#> 1 A     B    
#> 2 A     C    
#> 3 C     A    
#> 4 C     D

相关问题