Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Is there any R function for finding index of days of week, e.g., 1 for Sunday, 2 for Monday, & so on?

$
0
0

Two columns [say col1 and col5] of my data frame are containing days of the week such as Sunday, Monday, ....

I want to convert those columns to their respective indices such as '1' for 'Sunday, '2' for 'Monday', ... & so on. Currently, I'm using ifelse but I believe there might be an efficient way to do the same in R.

Sample code:

df$col1 = ifelse(df$col1=="Sunday",1,df$col1)
df$col1 = ifelse(df$col1=="Monday",2,df$col1)
....
df$col1 = ifelse(df$col1=="Saturday",7,df$col1)

And similar code for col5

df$col5 = ifelse(df$col5=="Sunday",1,df$col5)
df$col5 = ifelse(df$col5=="Monday",2,df$col5)
....
df$col5 = ifelse(df$col5=="Saturday",7,df$col5)

I tried looking for suitable function in lubridate package but couldn't find any.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>