Trying to put levels in data frame.

I hvae below data frame.

I am trying to put levels in below data so that first element shud be first date, which is 03-MAY-2021 in this case.

I use below code but it is not working.

dates_data$TIMESTAMP<-factor(x=dates_data$TIMESTAMP, levels=paste(formatC(x = 1:31,width = 2,flag = "0"),'-',rep(x = month.abb,each = 31),'-','2021'))

But it given NA in all values as output.

Thanks to advise.

dates_data
TIMESTAMP
1 01-JUN-2021
2 03-MAY-2021
3 04-MAY-2021
4 05-MAY-2021
5 06-MAY-2021
6 07-MAY-2021
7 10-MAY-2021
8 11-MAY-2021
9 12-MAY-2021
10 14-MAY-2021
11 17-MAY-2021
12 18-MAY-2021
13 19-MAY-2021
14 20-MAY-2021
15 21-MAY-2021
16 24-MAY-2021
17 25-MAY-2021
18 26-MAY-2021
19 27-MAY-2021
20 28-MAY-2021
21 31-MAY-2021

Do I understand that you're trying to create a factor type column?

How about dates_data$time_fac <- factor(dates_data$TIMESTAMP, unique(sort(dates_data$TIMESTAMP)))

Above code wont set the level of dates.

I tried below:
dates_data$time_fac <- factor(dates_data$TIMESTAMP,levels=paste(formatC(x = 1:31,width = 2,flag = "0"),'-',rep(x = month.abb,each = 31),'-','2021'))

But i am still getting below output:

TIMESTAMP time_fac

1 01-JUN-2021 NA
2 03-MAY-2021 NA
3 04-MAY-2021 NA
4 05-MAY-2021 NA
5 06-MAY-2021 NA
6 07-MAY-2021 NA
7 10-MAY-2021 NA
8 11-MAY-2021 NA
9 12-MAY-2021 NA
10 14-MAY-2021 NA

Thanks

Please do

dput(head(dates_data$TIMESTAMP))

And share here the result
Use triple backticks ``` on their own on a line above the result to format it as code.

I even tried below:

dates_data$TIMESTAMP<-as.Date(x = dates_data$TIMESTAMP,format = "%dd-%MMM-%yyyy")

Getting below output:

TIMESTAMP

1 NA
2 NA
3 NA
4 NA
5 NA
6 NA
7 NA
8 NA

This topic was automatically closed 21 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.