1

After I read a csv in RStudio, some columns were able to detect Null Values (NA) but others not. View(df)displays some columns with blank spaces & not as NA. How do I go about this so that null values can be represented as NA?

# Create example data frame
dat <- data.frame(
  Date = c("04/12/2011", "03/01/2002", "02/07/2002", "01/02/2001", "", ""),
  A = "",
  B = c(NA, 1981, NA, 1981, 1950, 1989)
)

enter image description here

www
  • 38,575
  • 12
  • 48
  • 84
andy
  • 1,947
  • 5
  • 27
  • 46
  • Please do not post screenshot. Next time when you ask a question, please create a reproducible example of your dataset, such as what I did in my post. – www May 01 '18 at 06:50

3 Answers3

4

While reading the csv itself you can specify to replace your blank cells by NA

df <-read.csv("data.csv", header=T, na.strings=c("","NA"))

If there are spaces in the blank cells, you can specify :

na.strings=c(""," ","NA")
Shubham R
  • 7,382
  • 18
  • 53
  • 119
3

Do you mean that some rows do not show any date?

You could try using

df[df == ""] = NA

as explained here

SamPer
  • 64
  • 6
1

We can use lapply and replace to replace "" with NA.

# Create example data frame
dat <- data.frame(
  Date = c("04/12/2011", "03/01/2002", "02/07/2002", "01/02/2001", "", ""),
  A = "",
  B = c(NA, 1981, NA, 1981, 1950, 1989)
)

dat
#         Date A    B
# 1 04/12/2011     NA
# 2 03/01/2002   1981
# 3 02/07/2002     NA
# 4 01/02/2001   1981
# 5              1950
# 6              1989

# Replace the "" with NA
dat[] <- lapply(dat, function(x){
  replace(x, x %in% "", NA)
})

dat
#         Date    A    B
# 1 04/12/2011 <NA>   NA
# 2 03/01/2002 <NA> 1981
# 3 02/07/2002 <NA>   NA
# 4 01/02/2001 <NA> 1981
# 5       <NA> <NA> 1950
# 6       <NA> <NA> 1989
www
  • 38,575
  • 12
  • 48
  • 84
  • 1
    As others have said you can set the missing values when you read in the csv file, if you already have your dataframe then you can use the handy `na_if` command from `dplyr` package `library(dplyr)` `?na_if` For your dataframe, something like; `dat$Date <- na_if(dat$Date, '')' – sorearm May 01 '18 at 09:05
  • 1
    @sorearm Thanks for the tips. – www May 01 '18 at 09:09