这是我的数据,包含在data.frame中,
rt5.5 <- c(0, 7, 1, 2, 2, 2, 1, 1) #LC trace % area
rt6.3 <- c(0, 16, 12, 20, 16, 20, 13, 15) #LC trace % area
rt8.4 <- c(0, 26, 55, 56, 63, 61, 70, 70) #LC trace % area
rt9.4 <- c(88, 42, 20, 11, 8, 6, 5, 3) #LC trace % area
rt22.7 <- c(8, 6, 7, 6, 7, 6, 7, 6) #LC trace % area
LCtraces <- data.frame(rt5.5, rt6.3, rt8.4, rt9.4,rt22.7,
row.names = c("A", "B", "C", "D", "E", "F", "G", "H"))
total.area <- apply(LCtraces,1, sum)
LCtraces <- merge(LCtraces,total.area, by="row.names")
colnames(LCtraces) [7] <- "Total Area"
rm(rt5.5, rt6.3, rt8.4, rt9.4, rt22.7, total.area) #tidy
当我构建data.frame时,我想在行之间添加总计:为此我使用了apply函数,然后使用row.names将结果向量与data.frame合并。然后我把我想要的列名称。
我所做的工作但缺乏优雅,任何人都可以提出任何改进建议吗?
答案 0 :(得分:4)
使用rowSums
和transform
:
LCtraces <- transform(LCtraces, total.area = rowSums(LCtraces))
等效地,使用within
:
LCtraces <- within(LCtraces, "Total Area" <- rowSums(LCtraces))