Question

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)
Was it helpful?

Solution 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.

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top