在 R dplyr 中按计数展开列
Spread columns by count in R dplyr
我有一个因子列。我想将每个因素分散到一列中,然后通过每个 id 出现的那个因素的计数来填补空白。假设我们有:
car <- c("a","b","b","b","c","c","a","b","b","b","c","c")
type <- c("good", "regular", "bad","good", "regular", "bad","good", "regular", "bad","good", "regular", "bad")
car_type <- data.frame(car,type)
并得到:
car type
1 a good
2 b regular
3 b bad
4 b good
5 c regular
6 c bad
7 a good
8 b regular
9 b bad
10 b good
11 c regular
12 c bad
我想要这个:
> results
car good regular bad
1 a 2 0 0
2 b 2 2 2
3 c 0 2 2
我尝试使用 dplyr,但我不太习惯,所以它不起作用。
car_type %>%
select(car, type) %>%
group_by(car) %>%
mutate(seq = unique(type)) %>%
spread(seq, type)
感谢任何帮助。
在 base R 中试试这个:
xtabs(~car+type, car_type)
# type
#car bad good regular
# a 0 2 0
# b 2 2 2
# c 2 0 2
或
table(car_type)
tidyr::pivot_wider
更新:
library(tidyverse)
car_type %>%
count(car, type) %>%
pivot_wider(names_from=type, values_from=n, values_fill=0)
原答案
与reshape2
:
library(reshape2)
dcast(car_type, car ~ type)
如果您要使用 dplyr
,代码将是:
dplyr
和 reshape2
car_type %>% count(car, type) %>%
dcast(car ~ type, fill=0)
dplyr
和 tidyr
car_type %>% count(car, type) %>%
spread(type, n, fill=0)
在每种情况下,count(car, type)
等同于
group_by(car, type) %>% tally
或
group_by(car, type) %>% summarise(n=n())
和data.table
library(data.table)
dcast(setDT(car_type), car ~ type, fill=0)
我有一个因子列。我想将每个因素分散到一列中,然后通过每个 id 出现的那个因素的计数来填补空白。假设我们有:
car <- c("a","b","b","b","c","c","a","b","b","b","c","c")
type <- c("good", "regular", "bad","good", "regular", "bad","good", "regular", "bad","good", "regular", "bad")
car_type <- data.frame(car,type)
并得到:
car type
1 a good
2 b regular
3 b bad
4 b good
5 c regular
6 c bad
7 a good
8 b regular
9 b bad
10 b good
11 c regular
12 c bad
我想要这个:
> results
car good regular bad
1 a 2 0 0
2 b 2 2 2
3 c 0 2 2
我尝试使用 dplyr,但我不太习惯,所以它不起作用。
car_type %>%
select(car, type) %>%
group_by(car) %>%
mutate(seq = unique(type)) %>%
spread(seq, type)
感谢任何帮助。
在 base R 中试试这个:
xtabs(~car+type, car_type)
# type
#car bad good regular
# a 0 2 0
# b 2 2 2
# c 2 0 2
或
table(car_type)
tidyr::pivot_wider
更新:
library(tidyverse)
car_type %>%
count(car, type) %>%
pivot_wider(names_from=type, values_from=n, values_fill=0)
原答案
与reshape2
:
library(reshape2)
dcast(car_type, car ~ type)
如果您要使用 dplyr
,代码将是:
dplyr
和 reshape2
car_type %>% count(car, type) %>%
dcast(car ~ type, fill=0)
dplyr
和 tidyr
car_type %>% count(car, type) %>%
spread(type, n, fill=0)
在每种情况下,count(car, type)
等同于
group_by(car, type) %>% tally
或
group_by(car, type) %>% summarise(n=n())
和data.table
library(data.table)
dcast(setDT(car_type), car ~ type, fill=0)