Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing NULL values in a data.frame

Tags:

r

I am quite new to R and I am working on a data frame with several NULL values. So far I am not able to replace those, and I can't wrap my head about a solution so it would be amazing if anybody could help me.

All the variables where the NULL value comes up are classified as factor.

If I use the function is.null(data) the answer is FALSE, which means that the have to replaced to be able to make a decent graph.

Can I use set.seed to replace all the NULL values, or I need to use a different function?

like image 205
L.Geerlofs Avatar asked Jun 13 '26 04:06

L.Geerlofs


1 Answers

You can use dplyr and replace

Data

df <- data.frame(A=c("A","NULL","B"), B=c("NULL","C","D"), stringsAsFactors=F)

solution

library(dplyr)

ans <- df %>% replace(.=="NULL", NA) # replace with NA

Output

     A    B
1    A <NA>
2 <NA>    C
3    B    D

Another example

ans <- df %>% replace(.=="NULL", "Z") # replace with "Z"

Output

  A B
1 A Z
2 Z C
3 B D
like image 104
CPak Avatar answered Jun 15 '26 11:06

CPak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!