I have a data frame like this: three columns, species, the compounds emitted by the species, and 1
in status
means the compound present in the species. And in this format, there is no 0
, which will indicate "absent".
我有一个这样的数据框架:三列,物种,物种发出的化合物,状态1表示物种中存在的化合物。在这种格式中,没有0,表示“缺席”。
Now, I want convert the data frame to a matrix like this: first column with species
, the rest of the columns with compounds
, and use 1
and 0
indicate "present" and "absent", respectively.
现在,我要将数据帧转换成这样的矩阵:第一列带有种,其余列带有化合物,使用1和0分别表示“当前”和“缺席”。
Is there a R code can do this? As you can see, there are so many rows in the original data frame. Thank you in advance!
有没有R代码可以做到这一点?如您所见,原始数据帧中有很多行。提前谢谢你!
1 个解决方案
#1
2
We can use dcast
我们可以使用dcast
library(reshape2)
dcast(data, species ~compounda, value.var = 'status', fill = 0)
If we need a matrix
use change dcast
to acast
如果我们需要一个矩阵使用改变dcast到acast。
#1
2
We can use dcast
我们可以使用dcast
library(reshape2)
dcast(data, species ~compounda, value.var = 'status', fill = 0)
If we need a matrix
use change dcast
to acast
如果我们需要一个矩阵使用改变dcast到acast。