7

我有一个因子列。我想将每个因素分散到一列中,然后通过每个 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)

我会感谢任何帮助。

4

2 回答 2

14

更新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,代码将是:

dplyrreshape2

car_type %>% count(car, type) %>%
  dcast(car ~ type, fill=0)

dplyrtidyr

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)
于 2016-11-06T21:04:52.467 回答
6

在基础 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)
于 2016-11-06T20:31:06.193 回答