Using the `is.na<-` in the lapply will work and preserve the types and sizes of columns and factors. Use do.call(data.frame,...) to recreate data.frame
do.call(data.frame, lapply(BAM, `is.na<-`)
for instance
BAM <- data.frame(x=1:5, y =rnorm(5), z = factor(letters[1:5])) blank_bam <- do.call(data.frame,lapply(BAM, `is.na<-`)) str(blank_bam) ## 'data.frame': 5 obs. of 3 variables: ## $ x: int NA NA NA NA NA ## $ y: num NA NA NA NA NA ## $ z: Factor w/ 5 levels "a","b","c","d",..: NA NA NA NA NA blank_bam ## xyz ## 1 NA NA <NA> ## 2 NA NA <NA> ## 3 NA NA <NA> ## 4 NA NA <NA> ## 5 NA NA <NA>
Saving some columns
If you only want to convert a numeric or integer (or any particular subset), run lapply on that subset of columns
For example, saving columns of factors.
I will filter to select only non-factor columns using Filter and is.factor
mnel source share