考虑到该数据集:
Name Height Weight
1 Mary 65 110
2 John 70 200
3 Jane 64 115
我想对每个限定列(身高和体重)求和,得出
199 425
问题在于限定词可能不止两个(即不仅限于身高和体重)。
我能做到这点。
# Create the dataframe people
Name <- c("Mary", "John", "Jane")
Height <- c(65,70,64)
Weight <- c(110,200,115)
people <- data.frame(Name, Height, Weight)
res <- c(sum(people$Height),sum(people$Weight))
当限定词增加时,句子变得太长了。 有没有更简洁的方法?
colSums
高效,而且打字时间更长。 - Gregor Thomas