[英]R. I am trying to subset my data frame by decades. Therefore I want to subset by using values of a column
I have the year column from 1921 to 2020. I want to make an analysis based on the decades, so I want to subset the data frame into decades.我有从 1921 年到 2020 年的年份列。我想根据几十年进行分析,所以我想将数据框子集为几十年。 I tried couple of codes but they keep giving errors.
我尝试了几个代码,但他们不断给出错误。
decade1=data_all%>%filter(data_all$year%>%1920:1929)
Error: Problem with
filter()
input..1
.错误:
filter()
输入..1
有问题。 x 3 arguments passed to ':' which requires 2 ℹ Input..1
isdata_all$year %>% 1920:1929
.x 3 arguments 传递给 ':' 这需要 2 ℹ 输入
..1
是data_all$year %>% 1920:1929
。 Runrlang::last_error()
to see where the error occurred.运行
rlang::last_error()
以查看错误发生的位置。
decade1=data_all%>%filter(data_all$year==1920:1929)
Warning message: In data_all$year == 1920:1929: longer object length is not a multiple of shorter object length
警告消息:在 data_all$year == 1920:1929 中:较长的 object 长度不是较短 object 长度的倍数
What code should I be using?我应该使用什么代码?
We can change the syntax to %in%
within filter
我们可以在
filter
中将语法更改为%in%
library(dplyr)
data_all%>%
filter(year %in% 1920:1929)
It may help to group the years first.
df <- data.frame(
year = sample(1920:2020,50,replace = TRUE)
)
df %>%
mutate( decade = cut(df$year, breaks=c(1910,1919,1929,1939,1949,1959,1969,1979,1989,1999,2009,2019,2029),
labels=c("1910s","1920s","1930s","1940s","1950s","1960s","1970s","1980s","1990s","2000s","2010s","2020s"))) %>%
arrange(year)
my solution of the exercise was (quite similar to yours):我对练习的解决方案是(与您的非常相似):
decades <- seq(1890,2010, by=10)
data$decade <- as.factor(data$Year %/% 10 * 10)
print(data$decade)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.