Set NA and “” Cells in R Dataframe to NULL

前端 未结 1 1318
暗喜
暗喜 2021-01-25 04:36

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<

1条回答
  •  温柔的废话
    2021-01-25 05:12

    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)) ) , ]
    

    0 讨论(0)
提交回复
热议问题