screechOwl
screechOwl

Reputation: 28139

R can't convert NaN to NA for large data frame

I have a decent size data set of ~ 60 columns that was accidentally populated with NaN's instead of NA's. The column types are a mix of character, numeric, factor, integer. I need to convert the NaN's to NA's as they are screwing up the works on several functions including linear regression. I am aware of how to change an individual column from this question here:

R can't convert NaN to NA

but am curious if there is a way to do this for a full data frame without losing the vector types. Any suggestions or is this a manual job?

Upvotes: 2

Views: 2033

Answers (3)

user4639132
user4639132

Reputation: 1

Using the above sample dataset. Try this:

CMBv = colnames(dat)

dat[CMBv] = lapply(dat[CMBv], function(x){ifelse(is.nan(x), NA,x)} )

Upvotes: -1

IRTFM
IRTFM

Reputation: 263332

Would this work ? ( It should for numeric, integer, character and factor vectors.)

as.data.frame( lapply(dat, function(col) {
                 if (is.numeric(col)) { is.na(col) <- is.nan(col); return(col)} else {
                 if (is.character(col) || is.factor(col) )  {
                                              is.na(col) <- col == "NaN"; return(col)} else {
                 return(col)                                                                }
                                                                                     }
                                          }
               )

dat <- 
structure(list(tester1 = structure(c(1L, 1L, 2L, 3L, 1L, 2L, 
4L), .Label = c("2", "3", "4", "NaN"), class = "factor"), tester2 = c(2, 
2, 3, 4, 2, 3, NaN)), .Names = c("tester1", "tester2"), row.names = c(NA, 
-7L), class = "data.frame")

# Produced:

  tester1 tester2
1       2       2
2       2       2
3       3       3
4       4       4
5       2       2
6       3       3
7    <NA>      NA

Upvotes: 1

aatrujillob
aatrujillob

Reputation: 4816

Would

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
     fixed = FALSE, useBytes = FALSE)

Work?

Maybe you would need a mix with apply. Could you provide a small example so I can try to implement it?

Thanks.

Upvotes: 1

Related Questions