Creating a frequency table in R

醉酒当歌 提交于 2019-12-11 11:35:32

问题


I am new to R, I want to create a frequency table, I have a data like this,

Member_id                   Car                 interest
1                    FORD_MUSTANG                 4
1                    BUICK_Lucerne                1
1                    CHEVROLET_SILVERADO          1
2                    CHEVROLET_SILVERADO          1
2                    FORD_MUSTANG                 2  
3                    FORD_MUSTANG                 6

I would like to have a frequency table like this:

MEmber_id       FORD_MUSTANG   BUICK_Lucerne   CHEVROLET_SILVERADO
1                  4             1               1
2                  2             0               1
3                  6             0               0

I have tried using table(Member_id,car), but it returns me values 1 values for each car make.

Appreciate any help.


回答1:


Try

library(reshape2) 
dcast(df, Member_id~Car, value.var="interest", fill=0)
#    Member_id BUICK_Lucerne CHEVROLET_SILVERADO FORD_MUSTANG
#1         1             1                   1            4
#2         2             0                   1            2
#3         3             0                   0            6

Or

library(tidyr) 
spread(df, Car, interest, fill=0)
#  Member_id BUICK_Lucerne CHEVROLET_SILVERADO FORD_MUSTANG
#1         1             1                   1            4
#2         2             0                   1            2
#3         3             0                   0            6

If you want to create the columns in the order you specified

 df$Car <- with(df, factor(Car, unique(Car)))
 spread(df, Car, interest, fill=0)
#  Member_id FORD_MUSTANG BUICK_Lucerne CHEVROLET_SILVERADO
#1         1            4             1                   1
#2         2            2             0                   1
#3         3            6             0                   0

data

 df <- structure(list(Member_id = c(1L, 1L, 1L, 2L, 2L, 3L), Car = c("FORD_MUSTANG", 
 "BUICK_Lucerne", "CHEVROLET_SILVERADO", "CHEVROLET_SILVERADO", 
 "FORD_MUSTANG", "FORD_MUSTANG"), interest = c(4L, 1L, 1L, 1L, 
 2L, 6L)), .Names = c("Member_id", "Car", "interest"), class = "data.frame", row.names = c(NA, 
-6L))


来源:https://stackoverflow.com/questions/26469975/creating-a-frequency-table-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!