嗨,我是R的新手,请耐心等待,
我的数据安排如此,
Length Seq X
28 GTGCACCGCAAGTGCTTCTAAGAAGGAT 19
28 TGCACCGCAAGTGCTTCTAAGAAGGATC 18
29 GTGCACCGCAAGTGCTTCTAAGAAGGATC 19
29 GTGCACCGCAAGTGCTTCTAAGAAGGATC 19
我用过
count(dF, vars=c("Length", "X"))
生成一个如下所示的freq表:
Length X freq
28 15 160
28 16 163
28 17 21
29 15 198
29 16 410
29 17 104
如何重新排列数据,使其看起来像这样?
Length 15 16 17 total
28 160 163 21 344
29 198 410 104 712
30 205 614 393 1212
Tot 2746 6564 2012 11322
(我知道这些值是错误的)
答案 0 :(得分:0)
如果您希望它看起来像您的示例:
# your data
df<- data.frame(Length = c(28, 28, 28, 29, 29, 29),
X = c(15, 16, 17, 15, 16, 17),
freq = c(160, 163, 21, 198, 410, 104))
使用此功能
require(reshape)
tabler <- function(a){
b <- cast(a, Length~X)
b <- cbind(b, rowSums(b))
b <- rbind(b, colSums(b))
colnames(b)[ncol(b)] <- b[nrow(b),1] <- "total"
return(b)
}
tabler(df)
返回:
Length 15 16 17 total
1 28 160 163 21 344
2 29 198 410 104 712
3 total 358 573 125 1056
答案 1 :(得分:0)
base R
选项
addmargins(xtabs(freq~Length+X, df1))
# X
#Length 15 16 17 Sum
# 28 160 163 21 344
# 29 198 410 104 712
# Sum 358 573 125 1056
df1 <- structure(list(Length = c(28L, 28L, 28L, 29L, 29L, 29L),
X = c(15L,
16L, 17L, 15L, 16L, 17L), freq = c(160L, 163L, 21L, 198L, 410L,
104L)), .Names = c("Length", "X", "freq"), class = "data.frame",
row.names = c(NA, -6L))