Creating a frequency table in R

2019-08-09 03:56发布

问题:

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))


标签: r frequency