简体   繁体   English

在R中将星期几转换为星期几

[英]Convert day of week number to weekday name in R

I have a column in a dataframe that contains the day number ( 0 through 6, 0=Sunday, 1=Monday, etc) and I need to convert that to the day name. 我在数据框中有一个包含日期编号的列(0到6,0 =星期日,1 =星期一,依此类推),我需要将其转换为日期名称。 How can I do this? 我怎样才能做到这一点?

Sample data: 样本数据:

df <- data.frame(day_number=0:6)

Simple way with dplyr. 使用dplyr的简单方法。

library(dplyr)

df <- data.frame(day_number=0:6)

df$day_number <- recode(df$day_number, 
       "0"="Sunday",
       "1"="Monday",
       "2"="Tuesday",
       "3"="Wednesday",
       "4"="Thursday",
       "5"="Friday",
       "6"="Saturday")

Treat the column as a factor with day name as label: 将列视为因素,并以日期名称作为标签:

x <- data.frame(wday=rep(0:6, 2))
x$wday_name <- factor(x$wday, levels=0:6,
                      labels=c("Sunday", "Monday", "Tuesday", "Wednesday",
                               "Thursday", "Friday", "Saturday"))
x
#    wday wday_name
# 1     0    Sunday
# 2     1    Monday
# 3     2   Tuesday
# 4     3 Wednesday
# 5     4  Thursday
# 6     5    Friday
# 7     6  Saturday
# 8     0    Sunday
# 9     1    Monday
# 10    2   Tuesday
# 11    3 Wednesday
# 12    4  Thursday
# 13    5    Friday
# 14    6  Saturday

If you need a character column, use as.character afterwards. 如果需要字符列, as.character以后使用as.character

Assume that you have this data frame: 假设您具有以下数据框:

dat = data.frame(daynumber = c(1,5,6,0))

Create a data frame that show the corresponding number to the day 创建一个数据框,以显示当天的相应数字

df <- data.frame(Number = 0:6,
                 Day = weekdays(x=as.Date(seq(7), origin="2017-10-01")))

Use match to match the value 使用匹配来匹配值

dat$daynumber = df$Day[match(dat$daynumber,df$Number)]

A bit late, but I was searching to find the same solution and figured there'd be someway to do this in lubridate . 有点晚了,但是我一直在寻找找到相同的解决方案,并认为在lubridatelubridate来做到这lubridate lubridate codes days 1 thru 7 but there's still a simple solution: lubridate 1天到第7天编码,但是仍然有一个简单的解决方案:

library(lubridate)

df <- data.frame(day_number=0:6) %>% # Your data
  mutate(t_day_number = day_number +1) %>% # Add 1 to your existing day number
  mutate(day_of_week  = wday(t_day_number, label = TRUE)) # Use wday and switch on labels

  day_number t_day_number day_of_week
1          0            1         Sun
2          1            2         Mon
3          2            3         Tue
4          3            4         Wed
5          4            5         Thu
6          5            6         Fri
7          6            7         Sat

You can use select thereafter to remove the columns you don't want. 之后,您可以使用select删除不需要的列。

If don't want to install any package, there is a good solution: 如果不想安装任何软件包,有一个好的解决方案:

First let's create a data.frame with your day system, with random variables, just for example: 首先,让我们用您的日间系统创建一个data.frame,它带有随机变量,例如:

dnum<-sample(0:6,10,replace=T)
var<-sample(LETTERS,10,replace=T)
df<-data.frame(dnum,var)
> df
   dnum var
1     3   K
2     4   D
3     4   L
4     3   J
5     6   A
6     2   W
7     3   A
8     3   W
9     2   D
10    4   K

You can create a data bank with the day of the week names: 您可以使用星期几名称创建数据库:

dnom<-c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
do<-0:6
dfo <- data.frame(dnom,do)
> dfo
       dnom do
1    Sunday  0
2    Monday  1
3   Tuesday  2
4 Wednesday  3
5  Thursday  4
6    Friday  5
7  Saturday  6

Now, use a loop to find compare the day numbers in your data.frame, with the data bank, and replace for day of week names. 现在,使用循环查找将data.frame中的日期与数据库进行比较,并替换为星期几名称。

sub<-c()
for(i in 1 :nrow(df)){
  d<-dfo[dfo$do==df$dnum[i],]
  sub[i]<-as.character(d$dnom[1])
}
df$dnum<-sub

        dnum var
1  Wednesday   K
2   Thursday   D
3   Thursday   L
4  Wednesday   J
5   Saturday   A
6    Tuesday   W
7  Wednesday   A
8  Wednesday   W
9    Tuesday   D
10  Thursday   K

It's not elegant, but I hope that helps you. 它不优雅,但希望对您有所帮助。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM