使用dplyr根据条件R过滤时间序列数据
我有一个时间序列数据,如下所示使用dplyr根据条件R过滤时间序列数据,r,dplyr,time-series,R,Dplyr,Time Series,我有一个时间序列数据,如下所示 keyword byMonth n_views business tax preparation software Dec-2016 5 corporate income tax solution Nov-2016 3 corporate income tax solut
keyword byMonth n_views
business tax preparation software Dec-2016 5
corporate income tax solution Nov-2016 3
corporate income tax solution Mar-2017 2
corporate tax provision Dec-2016 5
corporate tax provision Oct-2016 1
data collection Mar-2017 39
data collection May-2017 26
data collection Apr-2017 22
data collection Feb-2017 15
data collection Jan-2017 15
data collection Nov-2016 13
data collection Dec-2016 7
data collection Oct-2016 6
我只想使用dplyr或任何其他方便的方法选择那些贯穿2016年10月至2017年5月的
关键字。因此,在这种情况下,只有与关键字:数据收集
相关的观察才应该是输出。我很难弄明白这一点。非常感谢。我们可以使用dplyr
和tidyr
中的函数。dt2
中的关键字是全月覆盖的案例
library(dplyr)
library(tidyr)
dt2 <- dt %>%
# Spread the data frame
spread(byMonth, n_views) %>%
# Filter rows without any NA
filter(rowSums(!is.na(.)) == ncol(.))
数据准备
dt我们可以使用dplyr
和tidyr
中的函数。dt2
中的关键字是全月覆盖的案例
library(dplyr)
library(tidyr)
dt2 <- dt %>%
# Spread the data frame
spread(byMonth, n_views) %>%
# Filter rows without any NA
filter(rowSums(!is.na(.)) == ncol(.))
数据准备
dt第5行也是2016年10月
,是否也包括公司税规定
?否,仅包括2016年10月至2017年5月期间存在的关键字。不仅仅是几个月。因此,输出将只包含数据收集
第5行也是2016年10月
,是否也包括公司税准备金
?否,仅包括2016年10月至2017年5月期间存在的关键字。不仅仅是几个月。因此,输出将只有数据收集
我正在删除我的答案,因为这似乎是询问者真正想要的。好主意,但我使用输出绘制面积图,并使用x轴的byMonth
。所以这没用(@KrishnangKDalal请查看我的更新。过滤完成后,我们可以将格式转换回来。干杯!非常感谢:)我正在删除我的答案,因为这似乎是询问者真正想要的。好主意,但我正在使用输出绘制面积图,并使用x轴的byMonth
。所以这没用(@KrishnangKDalal请查看我的更新。过滤完成后,我们可以将格式转换回来。干杯!非常感谢:)
dt <- read.table(text = " keyword byMonth n_views
'business tax preparation software' 'Dec-2016' 5
'corporate income tax solution' 'Nov-2016' 3
'corporate income tax solution' 'Mar-2017' 2
'corporate tax provision' 'Dec-2016' 5
'corporate tax provision' 'Oct-2016' 1
'data collection' 'Mar-2017' 39
'data collection' 'May-2017' 26
'data collection' 'Apr-2017' 22
'data collection' 'Feb-2017' 15
'data collection' 'Jan-2017' 15
'data collection' 'Nov-2016' 13
'data collection' 'Dec-2016' 7
'data collection' 'Oct-2016' 6",
header = TRUE, stringsAsFactors = FALSE)