R语言数据框data.frame行和列求和方法

代码如下:

widgets <- c(179,153,183,153,154)
gadgets <- c(167,193,190,161,181)
thingys <- c(182,166,170,171,186)
daily.prod <- data.frame(widgets,gadgets,thingys,row.names = c('Mon','Tue','Wed','Thu','Fri'))
rbind(daily.prod,Total=colSums(daily.prod))
cbind(daily.prod,Totol=rowSums(daily.prod))

运行结果

  • 列求和
Date widgets gadgets thingys
Mon 179 167 182
Tue 153 193 166
Wed 183 190 170
Thu 153 161 171
Fri 154 181 186
Total 822 892 875
  • 行求和
Date widgets gadgets thingys Totol
Mon 179 167 182 528
Tue 153 193 166 512
Wed 183 190 170 543
Thu 153 161 171 485
Fri 154 181 186 521

你可能感兴趣的:(R,r语言)