Functions Not Applying to Entire Dataset

I wanted to get the date from a column that has both date and time. I applied the function and it worked but i think the function is not applying to the entire dataset because there are some records where i am not getting data rather i am getting an NA value.
I used the following lines of code

`all_trips$Date <- format(as.Date(all_trips$started_at, format = "%d/%m/%Y"), "%Y-%m-%d")

all_trips$Month <- format(as.Date(all_trips$Date),format = "%B")

all_trips$Day <- format(as.Date(all_trips$Date),format = "%A")

all_trips$Year <- format(as.Date(all_trips$Date),format = "%Y")`

And the code worked. Now if you look at the "started_at" column then the date and time both are available in that column but the date is not showing in the columns which i have circled in red color at the bottom.
What is the problem here? And how can i correct this problem?
Thank you.

In the screenshot you are showing started_at is in the ISO format YMD HMS not "%d/%m/%Y" that is why you get NAs because it can't parse the dates with the wrong format.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

But this problem is not appearing in the entire dataset. For example if i do the head(all_trips) then it shows the values which i am expecting. I am attaching the screenshot below.

Well, that means you have two date formats in your data, you should have normalized date formats before joining the datasets. This has nothing to do with a function not being applied to the entire data set but to the way you are processing your raw data.

I think it would be simpler to just reprocess your raw data files to make sure they all have the correct date format.

Basically, my data is in 4 separate excel files and i combined all of them together to create one final file all_tripsand i did a lot of data manipulation before binding all files together and just now i checked data type of started_at column of all separate excel files and each started_at column in all 4 excel files have the same data type which is "char"

What should i do now ? Can you recommend me anything?

Convert the started_at column in each dataframe to date type before combining them. Use a suitable date format for each case, some files are already in ISO format but some are not.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.