Function to replace spaces with NA

I am trying to write a function that turns empty strings into NA. A summary of one of my columns is as follows:

ab 12 210 468 

I would like to change 12 empty values ​​to NA. I also have several other factor columns for which I would like to change the empty values ​​to NA, so I borrowed something here and there to come up with this:

 # change nulls to NAs nullToNA <- function(df){ # split df into numeric & non-numeric functions a<-df[,sapply(df, is.numeric), drop = FALSE] b<-df[,sapply(df, Negate(is.numeric)), drop = FALSE] # Change empty strings to NA b<-b[lapply(b,function(x) levels(x) <- c(levels(x), NA) ),] # add NA level b<-b[lapply(b,function(x) x[x=="",]<- NA),] # change Null to NA # Put the columns back together d<-cbind(a,b) d[, names(df)] } 

However, I get this error:

 > foo<-nullToNA(bar) Error in x[x == "", ] <- NA : incorrect number of subscripts on matrix Called from: FUN(X[[i]], ...) 

I tried the answer found here: Replace all 0 values ​​with NA , but it changes all my columns to numeric values.

+8
source share
4 answers

You can directly index fields that match the logical criteria. Therefore, you can simply write:

 df[is_empty(df)] = NA 

Where is_empty is your comparison, for example. df == "" :

 df[df == ""] = NA 

But note that is.null(df) not working, and in any case will be weird 1 . However, I would advise you not to merge the logic for columns of different types! Instead, process them separately.


1 You almost never encounter NULL inside a table, since this only works if the underlying vector is list . You can create matrices and data.frames with this restriction, but then is.null(df) will never be TRUE , because NULL values ​​are enclosed in a list).

+10
source

How about just:

 df[apply(df, 2, function(x) x=="")] = NA 

Works great for me, at least with simple examples.

+1
source

It worked for me

  df[df == 'NULL'] <- NA 
0
source

This is the function I used to solve this problem.

 null_na=function(vector){ new_vector=rep(NA,length(vector)) for(i in 1:length(vector)) if(vector[i]== ""){new_vector[i]=NA}else if(is.na(vector[i])) {new_vector[i]=NA}else{new_vector[i]=vector[i]} return(new_vector) } 

Just plug in the column or vector you're having a problem with.

0
source

Source: https://habr.com/ru/post/1259153/


All Articles