具有字符向量的数据帧的快速聚合

时间:2018-10-16 15:24:30

标签: r dplyr data.table aggregate

给出以下数据框:

df <- structure(list(OTU = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L), class = "factor", .Label = c("OTU_1", "OTU_2", "OTU_3", 
"OTU_4", "OTU_5")), read = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L), .Label = c("a", "b", "c", "d", "e"), class = "factor")), class = "data.frame", row.names = c(NA, 
-25L))

     OTU read
1  OTU_1    a
2  OTU_2    a
3  OTU_3    a
4  OTU_4    a
5  OTU_5    a
6  OTU_1    b
7  OTU_2    b
8  OTU_3    b
9  OTU_4    b
10 OTU_5    b
11 OTU_1    c
12 OTU_2    c
13 OTU_3    c
14 OTU_4    c
15 OTU_5    c
16 OTU_1    d
17 OTU_2    d
18 OTU_3    d
19 OTU_4    d
20 OTU_5    d
21 OTU_1    e
22 OTU_2    e
23 OTU_3    e
24 OTU_4    e
25 OTU_5    e

我想创建一个新的数据框,如下所示:

       a    b   c   d   e
OTU_1  1    1   1   1   1
OTU_2  1    1   1   1   1

....

这不是一个完美的例子,因为数据框的值始终为1,但是在我的数据框中,您有不同数量的字母。

由于我的数据帧很大(150万行),我该如何快速完成?

谢谢

0 个答案:

没有答案