简体   繁体   English

R:根据 dplyr 的列值打破 data.frame

[英]R: Break a data.frame according to value of column with dplyr

I have this data.frame我有这个data.frame

MWE <- data.frame(x = c("a", "a", "a", "b", "b", "b"), y = c(1,2,3,4,5,6))

and what I want to obtain is this data.frame我想要得到的是这个data.frame

data.frame(a = c(1,2,3), b = c(4,5,6)) 

Actually, what I originally want is to sum the 2 vectors a and b (well, I have in reality many more vectors, but it is easier to explain with only 2), so that's why I thought about this transformation.实际上,我最初想要的是对 2 个向量ab sum (好吧,实际上我有更多向量,但只有 2 个更容易解释),所以这就是我考虑这种转换的原因。 I can do a rowSums then, or something equivalent.然后我可以做一个rowSums或类似的东西。

I tried to use pivot_wider from tidyr but I had an error.我尝试使用pivot_widertidyr ,但出现错误。

Any idea of how to do this with dplyr or tidyr ?知道如何使用dplyrtidyr做到这一点吗?

Continuing from @Mr.Flick's attempt in tidyverse you could create an id column and grouped on that id column calculate the sum like继续@Mr.Flick 在tidyverse中的尝试,您可以创建一个 id 列并在该 id 列上分组计算总和,如

library(dplyr)

MWE %>%
  group_by(x) %>%
  mutate(row = row_number()) %>%
  group_by(row) %>%
  mutate(total_sum = sum(y)) %>%
  tidyr::pivot_wider(names_from = x, values_from = y) %>%
  ungroup() %>%
  select(-row)

# A tibble: 3 x 3
#  total_sum     a     b
#      <dbl> <dbl> <dbl>
#1         5     1     4
#2         7     2     5
#3         9     3     6

We can use unstack from base R我们可以使用来自base R unstack unstack

unstack(MWE, y ~ x)
#  a b
#1 1 4
#2 2 5
#3 3 6

Or using rowid from data.table with pivot_wider from tidyr或者使用来自tidyrrowid和来自data.tablepivot_wider

library(dplyr)
library(data.table)
library(tidyr)
MWE %>% 
   mutate(rn = rowid(x)) %>% 
   pivot_wider(names_from = x, values_from = y) %>%
   select(-rn)
# A tibble: 3 x 2
#      a     b
#  <dbl> <dbl>
#1     1     4
#2     2     5
#3     3     6

Using base R:使用基础 R:

data.frame(with(MWE, split(y, x)))

  a b
1 1 4
2 2 5
3 3 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM