문제

I try to calculate the sum of each column for my data x. But I always got this error

"Error in colSums(x, na.rm = T) : invalid 'na.rm' argument"

Why the na.rm argument does not work in this case?? confused...

x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
x[3, ] <- NA; x[4, 2] <- NA
rowSums(x)
colSums(x, na.rm=T)
도움이 되었습니까?

해결책 2

I am able to recreate your bug by overriding the usual value of T to be equal to NA:

> T=NA
> colSums(all,na.rm=T)
Error in colSums(all, na.rm = T) : invalid 'na.rm' argument

So most likely, you (or a funny co-worker?) have defined the variable T somewhere in your code to be equal to NA. To undo it, just type:

T=TRUE

or better:

rm(T)

Never forget that R doesn't really know about T => it is just a shorthand defined for convenience at startup, nothing more.

다른 팁

You get an error because the value of T has been changed to an argument which is not interpretable as logical (TRUE or FALSE), can be NA or a character. In my opinion is a bad habit use T and F. To avoid errors:

colSums(x, na.rm=TRUE)

TRUE (or FALSE) cannot be overwritten, as reserved words.

Just for fun, you can try:

T = FALSE
F = TRUE

colSums(x, na.rm=T)
colSums(x, na.rm=F)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top