简体   繁体   中英

Cast to find frequency of pairs in “R”

If I have the following data:

id <- c(1,2,3,4,5,3,5,4)
type <- c(1,2,2,3,1,3,2,2)
df <- data.frame(id,type)

  id type
1  1    1
2  2    2
3  3    2
4  4    3
5  5    1
6  3    3
7  5    2
8  4    2

Suppose I want to get the frequency count of every (id,type) so that I get the following dataframe.

df.want <- data.frame(id = c(1,2,3,4,5),x.1 = c(1,0,0,0,1),x.2 = c(0,1,1,1,1),x.3 = c(0,0,1,1,0))

df.want

  id x.1 x.2 x.3
1  1   1   0   0
2  2   0   1   0
3  3   0   1   1
4  4   0   1   1
5  5   1   1   0

So for each ID I want the frequency of each type as a row. I tried cast(df,id ~ type,summary) but get:

Using type as value column.  Use the value argument to cast to override this choice
Error in `[.data.frame`(data, , variables, drop = FALSE) : 
  undefined columns selected

I think I might be close. Any ideas?

This is essentially a table operation:

as.data.frame.matrix(table(df))

#  1 2 3
#1 1 0 0
#2 0 1 0
#3 0 1 1
#4 0 1 1
#5 1 1 0

Try this:

> xtabs(data = df)
   type
id  1 2 3
  1 1 0 0
  2 0 1 0
  3 0 1 1
  4 0 1 1
  5 1 1 0

Using reshape

library(reshape)
df$type1 <- 1
cast(df, id~type,value="type1",fill=0)
#     id 1 2 3
#   1  1 1 0 0
#   2  2 0 1 0
#   3  3 0 1 1
#   4  4 0 1 1
#   5  5 1 1 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM