我找不到可复制的示例,所以这是我的数据:http://huitzi.fr/data/data.RData
该文件包含两个数据框。第一个是data
:
> str(data)
'data.frame': 53435 obs. of 6 variables:
$ Date : chr "2014-09-22T08:00:00" "2014-09-22T08:00:00" "2014-09-22T08:00:00" "2014-09-22T08:00:00" ...
$ Time.Spent..seconds.: int 408 288 91 40 16 9 7 5 4 2 ...
$ Number.of.People : int 1 1 1 1 1 1 1 1 1 1 ...
$ Activity : chr "reddit.com" "facebook.com" "kvirc" "Google Docs" ...
$ Category : chr "General Business" "General Social Networking" "General Communication & Scheduling" "Writing" ...
$ Productivity : int 2 -2 -2 2 2 -2 -1 1 0 0 ...
,第二个是data.copy
,它是data
的子集,只有6179行。
我正在尝试运行此代码:
> data <- mutate(data,
+ date = as.Date(substr(Date, 1, 10), "%Y-%m-%d"),
+ time = as.numeric(substr(Date, 12, 13)),
+ day = weekdays(date),
+ week = format(date, "%W"),
+ year = format(date, "%Y"),
+ month = paste(substr(months(date), 1, 3), substr(year, 3, 4)))
但是当我这样做时,R崩溃了(我在Windows上试过RStudio和R Gui)。
但是,如果我在data.copy上运行相同的代码,那么效果很好。另外,如果我这样做:
data$date <- as.Date(substr(data$Date, 1, 10), "%Y-%m-%d")
data$time <- as.numeric(substr(data$Date, 12, 13))
data$day <- weekdays(data$date)
data$week <- format(data$date, "%W")
data$year <- format(data$date, "%Y")
data$month <- substr(months(data$date), 1, 3)
data$month <- paste(data$month, substr(data$year, 3, 4))
它也很有效。但我想使用dplyr,因为我刚发现它并想要习惯它。