简体   繁体   English

使用两个变量创建计数矩阵

[英]Create matrix of counts using two variables

I have two columns - a unique id column id and the day of travel day . 我有两列-唯一的ID列id和旅行day My objective is to create a matrix of counts per id per day (and to include all days even if the count is zero) 我的目标是创建每天每个id的计数矩阵(并包括所有天数,即使计数为零)

> test
   id day
1   3   3
2   4   4
3   1   4
4   2   3
5   2   5
6   2   4
7   1   1
8   5   4
9   1   1
10  3   2
11  2   2
12  4   2
13  2   4
14  2   5
15  4   5
16  3   4
17  5   3
18  3   2
19  5   5
20  3   4
21  1   3
22  2   3
23  2   5
24  5   2
25  3   2

The output should be the following, where rows represent id and columns represent day : 输出应为以下内容,其中行代表id ,列代表day

> output
  1 2 3 4 5
1 2 0 1 1 0
2 0 1 2 2 3
3 0 3 1 2 0
4 0 1 0 1 1
5 0 1 1 1 1

I have tried the following with the reshape package 我已经尝试过以下与reshape包装

output <- reshape2::dcast(test, day ~ id, sum)

but it throws the following error: 但它引发以下错误:

Error in unique.default(x) : unique() applies only to vectors

Why does this happen and what would the right solution be in dplyr or using base R? 为什么会发生这种情况?在dplyr或使用基数R中正确的解决方案是什么? Any tips would be appreciated. 任何提示将不胜感激。

Here is the data: 数据如下:

> dput(test)
structure(list(id = c(3, 4, 1, 2, 2, 2, 1, 5, 1, 3, 2, 4, 2, 
2, 4, 3, 5, 3, 5, 3, 1, 2, 2, 5, 3), day = c(3, 4, 4, 3, 5, 4, 
1, 4, 1, 2, 2, 2, 4, 5, 5, 4, 3, 2, 5, 4, 3, 3, 5, 2, 2)), .Names = c("id", 
"day"), row.names = c(NA, -25L), class = "data.frame")

Easier to see whats going on with character variables 更容易了解字符变量的情况

id <- c('a', 'a', 'b', 'f', 'b', 'a')
day <- c('x', 'x', 'x', 'y', 'z', 'x')

test <- data.frame(id, day)



output <- as.data.frame.matrix(table(test))

This is the simplest way to do it...use the table() function then convert to data.frame 这是最简单的方法...使用table()函数然后转换为data.frame

 ans <- tapply(test$id, test$day, 
               function(x) {
                 y <- table(x)
                 z <- rep(0, 5)
                 z[as.numeric(names(y))] <- y
                 z
               } )
 do.call("cbind", ans)
     1 2 3 4 5
[1,] 2 0 1 1 0
[2,] 0 1 2 2 3
[3,] 0 3 1 2 0
[4,] 0 1 0 1 1
[5,] 0 1 1 1 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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