如何在不同列组合中使用Pivot Wider?

wecizke3  于 2023-04-03  发布在  其他
关注(0)|答案(2)|浏览(96)
df <- data.frame(xy=c("A","A","B","B","C","C"), xz=c("D","E","F","G","H","I"), y=c(1,2,3,4,5,6), z=c(1,2,3,4,5,6))

我试着用yxz分别对列xyz进行pivot_wider运算,结果如下:

y.A  y.B  y.C  z.D  z.E  z.F  z.G  z.H  z.I
3     7   11    1    2    3    4    5    6

xy==Axy==Bxy==C时有两个匹配项,所以我对这些字母中的每一个的y值求和,每个字母只得到一列。相反,因为xz字母都是唯一的,求和只是返回原始值。
我得到的最接近的结果是:

df %>% pivot_wider(names_from=c(xy,xz), values_from=c(y,z), names_sep='.', values_fn=sum)

但这会返回所有的组合

y.A.D y.A.E y.B.F y.B.G y.C.H y.C.I z.A.D z.A.E z.B.F z.B.G z.C.H z.C.I
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     1     1     2     2     3     3     1     2     3     4     5     6
vnzz0bqm

vnzz0bqm1#

一定有更优雅的东西:

bind_rows(df %>% select(col = xy, y), df %>% select(col = xz, z)) %>%
  pivot_longer(-col, values_drop_na = TRUE) %>%
  pivot_wider(names_from = c(name, col), values_from = value, values_fn = sum)

# A tibble: 1 × 9
    y_A   y_B   y_C   z_D   z_E   z_F   z_G   z_H   z_I
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     3     7    11     1     2     3     4     5     6
osh3o9ms

osh3o9ms2#

我的尝试是值得的...

df %>%
  summarise(name = list(c(xy, xz)),
            value = list(c(y, z)),
            label = list(rep(c("y", "z"), each = 6))) %>%
  unnest(c(name, value, label)) %>%
  pivot_wider(names_from = c(label, name), names_sep = ".", values_fn = sum)
# A tibble: 1 × 9
    y.A   y.B   y.C   z.D   z.E   z.F   z.G   z.H   z.I
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     3     7    11     1     2     3     4     5     6

相关问题