格式化R中的星期几

时间:2016-07-23 22:53:13

标签: r strftime strptime as.date

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

> 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"

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我认为这是相关的:

## 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)