[英]How to return the sum of even row numbers in R data frame?
I am very new to coding.我对编码很陌生。
I'm looking to retrieve frequency values of rows in my data frame我正在寻找在我的数据框中检索行的频率值
I already know you can do this using:我已经知道您可以使用以下方法执行此操作:
df$col <- rowSums( data[,0:100] )
But I specifically want the sum of data from rows that are divisible by two, in other words, even rows up to a specific point in my data frame.但我特别想要来自可被二整除的行的数据总和,换句话说,甚至是直到我的数据框中特定点的行。
Perhaps you would need to incorporate an if else function?也许您需要合并一个 if else 函数? Something vaguely similar to this oversimplified code?
有点类似于这个过于简化的代码?
if df$col[0:5]%%2
print rowSum
else:
don't
Anyone have any ideas?谁有想法?
Much appreciated非常感激
使用逻辑向量和回收规则进行索引将给出很好的解决方案:
rowSums(cars[c(FALSE, TRUE), ])
st.mat <- matrix(1:100,ncol = 10, nrow =10)
for(i in 1:dim(st.mat)[1]){
if(i %% 2 == 0){
print(sum(st.mat[i,]))
}
}
This would be a very simple way to do it.这将是一种非常简单的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.