0

I have a column in my data frame that contains dates so it is a date class. However, when I try to remove NA's from this column I cannot because it is not a character class. So I convert it to character class using this solution:

Setting to Blank

Then I still want my date column to be a date class so I convert it back using as.Date but then it generates NA's again. So I'm stuck in this loop. If an example is needed I will add it after my next meeting. I want to convert NA's to blanks because I'm using rbind to another dataframe that does not have NA's.

Below is the code I am referring to:

> df1
        Date File
1 2016-10-20    1
2 2016-10-18    2
3       <NA>    3

> str(df1)
'data.frame':   3 obs. of  2 variables:
 $ Date: Date, format: "2016-10-20" "2016-10-18" NA
 $ File: chr  "1" "2" "3"

> df1 <- sapply(df1, as.character)

> df1[is.na(df1)] <- ""

> df1
     Date         File
[1,] "2016-10-20" "1" 
[2,] "2016-10-18" "2" 
[3,] ""           "3" 

> df1 <- as.data.frame(df1)

> df1$Date <- as.Date(df1$Date, format = "%Y-%m-%d")

> df1
        Date File
1 2016-10-20    1
2 2016-10-18    2
3       <NA>    3

So I just want to know if the df1[1,3] can be a Date class and be blank.

acylam
  • 18,231
  • 5
  • 36
  • 45
d84_n1nj4
  • 1,712
  • 6
  • 23
  • 40

1 Answers1

0

Just try na.omit(df1), it will remove all the rows with NA values..