R 问题汇总列
我有一个非常大的数据集,我正试图得到值的总和。变量是二进制的,带有0和1 不知怎的,当我运行for循环时R 问题汇总列,r,R,我有一个非常大的数据集,我正试图得到值的总和。变量是二进制的,带有0和1 不知怎的,当我运行for循环时 for (i in 7:39){ agegroup1[53640, i]<-sum(agegroup1[, i]) } for(我在7:39){ 年龄组1[53640,i] 在开始添加列总和之前,agegroup1[53640,]中有什么内容?NA?如果是这样,可以解释一些行为 我们确实需要更多的细节 在开始添加列总和之前,agegroup1[53640,]中有什么内容?NA?如果是
for (i in 7:39){
agegroup1[53640, i]<-sum(agegroup1[, i])
}
for(我在7:39){
年龄组1[53640,i]
在开始添加列总和之前,agegroup1[53640,]
中有什么内容?NA
?如果是这样,可以解释一些行为
我们确实需要更多的细节
在开始添加列总和之前,agegroup1[53640,]
中有什么内容?NA
?如果是这样,可以解释一些行为
我们确实需要更多的细节,尽管…@Gavin Simpson提供了一个可行的解决方案,但您也可以使用apply。此函数允许您将函数应用于行或列边距
x <- cbind(x1=1, x2=c(1:8), y=runif(8))
# If you wanted to sum the rows of columns 2 and 3
apply(x[,2:3], 1, sum, na.rm=TRUE)
# If you want to sum the columns of columns 2 and 3
apply(x[,2:3], 2, sum, na.rm=TRUE)
x@Gavin Simpson提供了一个可行的解决方案,但您也可以使用apply。此函数允许您将函数应用于行或列边距
x <- cbind(x1=1, x2=c(1:8), y=runif(8))
# If you wanted to sum the rows of columns 2 and 3
apply(x[,2:3], 1, sum, na.rm=TRUE)
# If you want to sum the columns of columns 2 and 3
apply(x[,2:3], 2, sum, na.rm=TRUE)
x您检查过要求和的列中的NA值吗?您可以用any(is.NA(agegroup1))
检查NA
s,您可以用哪个(is.NA(agegroup1))
查看它们在哪里。您检查过要求和的列中的NA值吗?您可以用any(is.NA(agegroup1))检查NA
s
,您可以使用查看它们的位置,即.na(agegroup1))
。
x <- cbind(x1=1, x2=c(1:8), y=runif(8))
# If you wanted to sum the rows of columns 2 and 3
apply(x[,2:3], 1, sum, na.rm=TRUE)
# If you want to sum the columns of columns 2 and 3
apply(x[,2:3], 2, sum, na.rm=TRUE)