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

  • A+
Category:Languages

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?

 


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 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: