[英]data.frame conversion in r
I have a df with a bunch of columns. 我有一堆列的df。 Each row represents a species seen for each sampling trip. 每行代表每次采样行程所见的物种。 I want to convert this to a matrix or dataframe where each column is a species and each row is a sampling trip. 我想将其转换为矩阵或数据框,其中每列是一个物种,每行都是一个采样行程。 I want to convert it for analysis using vegan functions. 我想使用素食函数将其转换为分析。 I basically want the opposite of this melting data.frame in R 我基本上想要与R中的这个融合数据相反
Original format 原始格式
data.frame(speciesname=c("a","b","c","a"),sample.id=c(1,1,2,3),count=c(10,1,5,2))
speciesname sample.id count
1 a 1 10
2 b 1 1
3 c 2 5
4 a 3 2
I want to convert it to look like this: 我想将其转换为如下所示:
a b c
1 10 1 0
2 0 0 5
3 2 0 0
I am trying not to make some hideous double for loop with if statements but if that is what I have to do... 我试图不用if语句做一些可怕的双循环,但如果这是我必须要做的...
Using xtabs()
使用xtabs()
xtabs(count ~ sample.id + speciesname, df)
# speciesname
# sample.id a b c
# 1 10 1 0
# 2 0 0 5
# 3 2 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.