I have a tibble, named Alabama, with 31 rows and I want a new tibble made of only rows 1, 11, 21, 31. These rows have are in a column named Year and have the values 1970, 1980, 1990, 2000
I have tried
al_decades <- filter(Alabama, Year==1970 && Year==1980 && Year==1990 && Year==2000)
and
al_decades <- subset(Alabama, Year==1970 && Year==1980 && Year==1990 && Year==2000)
but neither has worked.
We need %in%
instead of ==
library(dplyr)
new <- filter(Alabama, Year %in% c(1970, 1980, 1990, 2000))
Or instead of &
(not &&
), use |
to check for multiple elements as 'Year' wouldn't have different years in the same row
new <- filter(Alabama, (Year == 1970)|(Year == 1980)|(Year == 1990)|(Year == 2000))
Or if we are only interested in the decades, then another option is
new <- filter(Alabama, Year %%10 == 0)
If we know the position, another option is slice
new <- Alabama %>%
slice(c(1, 11, 21, 31))
Or use filter
with row_number()
new <- Alabama %>%
filter(row_number() %in% c(1, 11, 21, 31))
Or using data.table
library(data.table)
setDT(Alabama, key = 'Year')[J(c(1970, 1980, 1990, 2000))]
In base R
, we can use subset
subset(Alabama, Year %in% c(1970, 1980, 1990, 2000))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments