3

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)
Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
ToToRo
  • 373
  • 1
  • 5
  • 12
  • 1
    `T` is NOT a reserved word in R. One can (and apparently you have) overwritten its value. Running `rm(T)` will work. But the best approach is to stick to `TRUE` and `FALSE` – Ricardo Saporta Mar 10 '14 at 02:49

2 Answers2

4

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)
Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
3

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.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Jealie
  • 6,157
  • 2
  • 33
  • 36