Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A function to fill in a column with NA of the same type

Tags:

r

na

dplyr

I have a data frame with many columns of different types. I would like to replace each column with NA of the corresponding class.

for example:

df = data_frame(x = c(1,2,3), y = c("a", "b", "c"))

df[, 1:2] <- NA

yields a data frame with two logical columns, rather than numeric and character. I know I can tell R:

df[,1] = as.numeric(NA)
df[,2] = as.character(NA)

But how do I do this collectively in a loop for all columns with all possible types of NA?

like image 943
Omry Atia Avatar asked Dec 11 '18 07:12

Omry Atia


2 Answers

You can use this "trick" :

df[1:nrow(df),1] <- NA
df[1:nrow(df),2] <- NA

the [1:nrow(df),] basically tells R to replace all values in the column with NA and in this way the logical NA is coerced to the original type of the column before replacing the other values.

Also, if you have a lot of columns to replace and the data_frame has a lot of rows, I suggest to store the row indexes and reuse them :

rowIdxs <- 1:nrow(df)
df[rowIdxs ,1] <- NA
df[rowIdxs ,2] <- NA
df[rowIdxs ,3] <- NA
...

As cleverly suggested by @RonakShah, you can also use :

df[TRUE, 1] <- NA
df[TRUE, 2] <- NA
...

As pointed out by @Cath both the methods still work when you select more than one column e.g. :

df[TRUE, 1:3] <- NA
# or
df[1:nrow(df), 1:3] <- NA
like image 119
digEmAll Avatar answered Nov 15 '22 19:11

digEmAll


Another solution that applies to all the columns can be to specify the non-NAs and replace with NA, i.e.

df[!is.na(df)] <- NA

which gives,

# A tibble: 3 x 2
      x    y    
  <dbl> <chr>
1    NA <NA> 
2    NA <NA> 
3    NA <NA> 
like image 28
Sotos Avatar answered Nov 15 '22 20:11

Sotos