Xbe*_*bel 5 r spread dataframe 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)
Run Code Online (Sandbox Code Playgroud)
得到:
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
Run Code Online (Sandbox Code Playgroud)
我要这个:
> results
car good regular bad
1 a 2 0 0
2 b 2 2 2
3 c 0 2 2
Run Code Online (Sandbox Code Playgroud)
我尝试使用dplyr,但我并没有真正使用它,所以它不起作用.
car_type %>%
select(car, type) %>%
group_by(car) %>%
mutate(seq = unique(type)) %>%
spread(seq, type)
Run Code Online (Sandbox Code Playgroud)
我会感谢任何帮助.
eip*_*i10 10
reshape2:library(reshape2)
dcast(car_type, car ~ type)
Run Code Online (Sandbox Code Playgroud)
如果您打算使用dplyr,代码将是:
dplyr 和 reshape2
car_type %>% count(car, type) %>%
dcast(car ~ type, fill=0)
Run Code Online (Sandbox Code Playgroud)
dplyr 和 tidyr
car_type %>% count(car, type) %>%
spread(type, n, fill=0)
Run Code Online (Sandbox Code Playgroud)
在任何一种情况下,count(car, type)相当于
group_by(car, type) %>% tally
Run Code Online (Sandbox Code Playgroud)
要么
group_by(car, type) %>% summarise(n=n())
Run Code Online (Sandbox Code Playgroud)
data.tablelibrary(data.table)
dcast(setDT(car_type), car ~ type, fill=0)
Run Code Online (Sandbox Code Playgroud)
在基础R中试试这个:
xtabs(~car+type, car_type)
# type
#car bad good regular
# a 0 2 0
# b 2 2 2
# c 2 0 2
Run Code Online (Sandbox Code Playgroud)
要么
table(car_type)
Run Code Online (Sandbox Code Playgroud)