I have read a csv file in as mydata, an existing column called inbound_date, contain the data like
NULL
2017-06-24 16:47:35
2017-06-24 16:47:35
I want to create a new column to extract the day for this column. i have tried below code, but failed,
mydata$inbound_day<-ifelse(is.null(mydata$inbound_date),"null",as.Date(mydata$inbound_date,format = "%Y-%m-%d"))
The new column inbound_day has been added, but it shows as NA in the column for all the rows.
Can help to see the code, which part is wrong? Thanks!
There are two things at play here.
The behaviour of ifelse
. It will return as many values as the
length of the condition. If the condition returns only one value, ifelse
too will return a single value.
The behaviour of is.null
is not the same as that of is.na
. Unlike is.na
, is.null(mydata$inbound_date)
is checking the whole
of mydata$inbound_date1
as a single object and you are getting
just one value in return, which is False
.
The combined effect of these two things is that you are only getting the as.Date
value for the first item as result, and it is a single NA
. What's more, this `NA is then being recycled to fill the whole column with NAs.
Solution -- Use is.na
where you are using is.null
. It will return multiple values and the thing will work as expected.