我的数据框中有一个变量,用于星期几 .
> 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"
任何帮助表示赞赏!