【发布时间】:2017-12-07 14:37:30
【问题描述】:
我有一个小数据集,我正在尝试使用 grepl 函数对 data.frame 进行子集化。
我有;
year_list <- list("2013", "2014", "2015", "2016", "2017")
test.2013 <- subset(searches[, 1:2], grepl(year_list[1], searches$date))
test.2014 <- subset(searches[, 1:2], grepl(year_list[2], searches$date))
test.2015 <- subset(searches[, 1:2], grepl(year_list[3], searches$date))
test.2016 <- subset(searches[, 1:2], grepl(year_list[4], searches$date))
test.2017 <- subset(searches[, 1:2], grepl(year_list[5], searches$date))
我正在尝试创建一个循环,以便将第 1 列到第 2 列(date 列和 hits 列)子集化为新的 data.frame。
我正在尝试使用year_lists 中的date,将grepl 函数应用于searches data.frame 中的date 列,并将这些值返回到新的data.frame,但使用循环函数或更少的函数比我现在的重复。
数据框
date hits keyword geo gprop category
1: 2013-01-06 23 Price world web 0
2: 2013-01-13 23 Price world web 0
3: 2013-01-20 40 Price world web 0
4: 2013-01-27 25 Price world web 0
5: 2013-02-03 21 Price world web 0
6: 2013-02-10 19 Price world web 0
【问题讨论】:
-
您正在使用 data.table-object。
-
library("lubridate"); searches[, Year:=year(as.Date(date))]... 现在您可以使用split(searches, searches[, Year])... 或者最终您想使用data.table的by=参数进行进一步计算。
标签: r