【问题标题】:Formatting days of the week in R在 R 中格式化星期几
【发布时间】:2016-11-27 13:50:03
【问题描述】:

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

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

感谢任何帮助!

【问题讨论】:

  • 您可以手动决定变量的因子顺序,对您来说不是吗?

标签: r strftime strptime as.date


【解决方案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)

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 2022-07-21
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多