Questions tagged [na.rm]

48 questions
29
votes
4 answers

Is it possible to set na.rm to TRUE globally?

For commands like max the option na.rm is set by default to FALSE. I understand why this is a good idea in general, but I'd like to turn it off reversibly for a while -- i.e. during a session. How can I require R to set na.rm = TRUE whenever it is…
Hugh
  • 15,521
  • 12
  • 57
  • 100
18
votes
1 answer

How to pass na.rm=TRUE to sapply when calculating median?

I have created a dataframe "killers" with 3 variables. The data are numeric though there exist NA values throughout. My goal is to calculate the mean on each of the 3 variables. sapply(killers, function(x) median) This…
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
13
votes
2 answers

R: summarise multiple column (numeric, character) and remove NAs

I have a data.frame with many columns (~50). Some of them are character, some are numeric and 3 of them I use for grouping. I need to: remove NAs from numeric columns calculate the mean of each of the numeric columns extract the first element of…
rpl
  • 451
  • 4
  • 13
12
votes
2 answers

How to pass na.rm as argument to tapply?

I´d like to calculate mean and sd from a dataframe with one column for the parameter and one column for a group identifier. How can I calculate them when using tapply? I could use sd(v1, group, na.rm=TRUE), but can´t fit the na.rm=TRUE into the…
Doc
  • 358
  • 1
  • 4
  • 24
11
votes
2 answers

NaN is removed when using na.rm=TRUE

This reproducible example is a very simplified version of my code: x <- c(NaN, 2, 3) #This is fine, as expected max(x) > NaN #Why does na.rm remove NaN? max(x, na.rm=TRUE) > 3 To me, NA (missing value) and NaN (not a number) are two completely…
Nishanth
  • 6,932
  • 5
  • 26
  • 38
6
votes
1 answer

data.table 1.8.x mean() function auto removing NA?

Today I found out a bug in my program due to data.table auto remove NA for mean for example: > a<-data.table(a=c(NA,NA,FALSE,FALSE), b=c(1,1,2,2)) > a > a[,list(mean(a), sum(a)),by=b] b V1 V2 1: 1 0 NA // Why V1 = 0 here? I had expected NA 2: 2…
colinfang
  • 20,909
  • 19
  • 90
  • 173
3
votes
1 answer

melt.data.table with na.rm for first element of list of measure.vars

I would like to explore the best way to melt a data.table with na.rm applying only for the first element of the list of measure.vars. I have a data.table as follows: library(data.table) library(lubridate) dt.master <- data.table(user = seq(1,5), …
illanxr
  • 65
  • 5
3
votes
3 answers

Issue with NA values in R

I feel this should be something easy, I have looked x the internet, but I keep getting error messages. I have done plenty of analytics in the past but am new to R and programming. I have a pretty basic function to calculate means x columns of…
Adam_S
  • 687
  • 2
  • 12
  • 24
3
votes
2 answers

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

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,…
ToToRo
  • 373
  • 1
  • 5
  • 12
2
votes
1 answer

Use of na.rm=True and na.action=na.pass in aggregate() - difference?

I couldn't find a question here on Stack overflow that already answers my question so I'm very sorry if this has already been asked and I just couldn't find it. All in all, this question is more about understanding what happens with my data…
Arya
  • 29
  • 2
2
votes
1 answer

na.rm = T treatment by ggplot2's geom_bar

I am trying to understand how ggplot2's geom_bar treats NAs. The help file says: library(ggplot2) ?geom_bar na.rm: If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed. I am trying it…
user3245256
  • 1,842
  • 4
  • 24
  • 51
2
votes
2 answers

rollapply na.rm = TRUE giving 0 values instead of NA's

I have a simple question, that I can't seem to find the answer to on google, stackoverflow or stackexchange. I am currently working with the example of rollapply to find the sum of some values that contain NA's. For example: z <- zoo(c(NA, NA,…
lurodrig
  • 99
  • 3
  • 8
2
votes
2 answers

Getting error in R trying to calculate mean on column that contains NA

From searching online and in this group, it seems like this should work: > mean(r_lab$ozone, na.rm=TRUE) However, what I get is: [1] NA Warning message: In mean.default(r_lab$ozone, na.rm = TRUE) : argument is not numeric or logical: returning…
David
  • 115
  • 3
  • 16
2
votes
2 answers

Remove NAs when using mapply for ttest in R

I would like to do a column-wise ttest between two dataframes in R. That is, ttest(df1$col1,df2$col1) ,ttest(df1$col2,df2$col2) and so on....The best option here is to use mapply or Map function. Something…
code123
  • 2,082
  • 4
  • 30
  • 53
2
votes
2 answers

Special use of colSums(), na.rm = TRUE only if 1 or fewer are missing

I need to sum some columns in a data.frame with a rule that says, a column is to be summed to NA if more than one observation is missing NA if only 1 or less missing it is to be summed regardless. Say I have some data like this, dfn <- data.frame( a…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
1
2 3 4