brno792
brno792

Reputation: 6799

Set NA and "" Cells in R Dataframe to NULL

I have a Dataframe in R where certain rows in some of the columns have values that are NA or empty strings "". I want to convert these to NULL values.

So I need any cells in my data frame that are NA or "" to be NULL. How can I do this?

When I try:

DF[ , DF$Column == NA] <- NULL

or

DF[ , DF$Column == ""] <- NULL

I get the error: missing values are not allowed in subscripted assignments of data frames

If I try:

DF[ , is.na(DF$Column)] <- NULL

I get the error: duplicate subscripts for columns

If I try:

is.na(DF$Column) <- NULL

or

DF[DF == NA] <- NULL

I dont get any errors, but nothing changes in my dataframe.

Upvotes: 1

Views: 13188

Answers (1)

IRTFM
IRTFM

Reputation: 263441

There really is no NULL value in a vector. NA is the placeholder. If you want to remove the entire column (which is what assigning NULL would do) when its values are all NA then this would succeed:

df[ , sapply(df, function(x) all(is.na(x)) ) ] <- NULL

If you want to construct an object where you keep only those rows with no NA values:

df[ apply(df,1, function(rw) !any(is.na(rw)) ) , ]

Upvotes: 1

Related Questions