在 R 中格式化星期几

Formatting days of the week in R

我的数据框中有一个星期几的变量。

> str(g.2015.1990$DAY.OF.WEEK)
 Factor w/ 7 levels "Friday","Monday",..: 1 3 4 2 6 7 5 1 3 4 ...

R 将此视为一个因素,但是否已经有我可以使用的特定星期几格式?我读过有关生成星期几或为已有日期指定星期几的问题;但是,我还没有读到任何关于将您已经拥有的变量的格式更改为星期几的内容。

这可能最终与我的研究无关;但是,如果格式正确,我会感觉更好。我看不出这会出现在哪里;但是,如果排序曾经成为一个问题,R 会按字母顺序(星期五、星期一、星期六等)对因子变量进行排序,显然,时间顺序(星期日、星期一、星期二等)是可取的。

这是我试过的方法:

dayx = as.Date(g.2015.1990$DAY.OF.WEEK, format = "%A")
dayx = as.Date(as.character(g.2015.1990$DAY.OF.WEEK), format = "%A")
dayx = strptime(g.2015.1990$DAY.OF.WEEK, format = "%A")
dayx = strftime(as.character(g.2015.1990$DAY.OF.WEEK, format = "%A"))
dayx = strptime(g.2015.1990$DAY.OF.WEEK, format = "%a")
dayx = as.Date(g.2015.1990$DAY.OF.WEEK, format = "%a")
dayx = as.Date(as.character(g.2015.1990$DAY.OF.WEEK), format = "%a")
dayx = strftime(as.character(g.2015.1990$DAY.OF.WEEK, format = "%a"))
dayx = strptime(sprintf('%s %04d', g.2015.1990$DATE, g.2015.1990$START.TIME, g.2015.1990$DAY.OF.WEEK), '%Y-%m-%d %H%M %a')

每个人似乎只是用今天的日期替换每个观察结果:

> dayx = as.Date(g.2015.1990$DAY.OF.WEEK, format = "%A")
> dayx[1:25]
 [1] "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23"
 [6] "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23"
[11] "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23"
[16] "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23"
[21] "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23" "2016-07-23"

感谢任何帮助!

我认为这是相关的:

## This is the order you desire
Weekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

## This simulates your `g.2015.1990$DAY.OF.WEEK`
set.seed(0); test <- factor(sample(Weekdays, 100, replace = TRUE))

## This simulates what you see from `str(g.2015.1990$DAY.OF.WEEK)`
str(test)
# Factor w/ 7 levels "Friday","Monday",..: 3 2 6 5 3 2 3 3 5 5 ...

## We can inspect levels
levels(test)
#[1] "Friday"    "Monday"    "Saturday"  "Sunday"    "Thursday"  "Tuesday"  
#[7] "Wednesday"

## This is what you should do to recode `test` for your desired order of levels
tmp <- levels(test)[as.integer(test)]  ## much more efficient than `tmp <- as.character(test)`
test <- factor(tmp, levels = Weekdays) ## set levels when using `factor()`

## This is what we see now
str(test)
# Factor w/ 7 levels "Sunday","Monday",..: 7 2 3 5 7 2 7 7 5 5 ...

levels(test)
# [1] "Sunday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
# [7] "Saturday" 

所以,总而言之,尝试:

Weekdays <- c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
tmp <- levels(g.2015.1990$DAY.OF.WEEK)[as.integer(g.2015.1990$DAY.OF.WEEK)]
## use `Weekdays` defined above
g.2015.1990$DAY.OF.WEEK <- factor(tmp, levels = Weekdays)