Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

How to replace values with NA in data frame or list without changing the class in R?

$
0
0

I want to fill the first n objects in my list or the first n columns in my data frame with NA values without changing the class of the objects. The problem is the following:

d <- data.frame(matrix(1:10, nrow=2, ncol=5))
d_NA <- d
d_NA[,1] <- NA
class(d[,1]) == class(d_NA[,1]) 

l <- list(1)
l_NA <- l
l_NA[[1]] <- NA

class(l[[1]]) == class(l_NA[[1]]) # outputs to false 

this happens because the NA that I fill into the data frame or list is of class "logical"

Now I could of course do it like this

d[,1] <- as.integer(NA)
l[[1]] <- as.integer(NA)

but I am looking for a more generic solution, that does not alter the class as long as it "has to".


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>