私は2つの列を持っています。一意のID列id
と旅行日day
です。2つの変数を使用してカウントの行列を作成します
id
を表し、列は
day
を表し、次の、あるべき
> 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
日あたりのIDあたりのカウントのマトリックスを作成するために(とカウントがゼロであっても、すべての日含めて)であります
> 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
は私がreshape
パッケージ
output <- reshape2::dcast(test, day ~ id, sum)
で次のことを試してみましたが、それは次のようなエラーがスローされます:
Error in unique.default(x) : unique() applies only to vectors
なぜこれが起こり、適切な解決策がdplyr
にあるのでしょうか、ベースRを使用していますか?任意のヒントをいただければ幸いです。ここで
はデータです:
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))
文字変数で起こっては、これはそれを行うための最も簡単な方法ですいただきました!見て
> 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")
'table(d)'は、あなたに必要な出力を与えます。 – Lamia
確かにこれは重複しています。 –