Subset tibble of four specific rows

Jay Wehrman

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.

akrun

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.

edited at
0

Comments

0 comments
Login to comment

Related

collapsing rows of a subset of a tibble

Subset a tibble by a smaller tibble

pandas - are specific rows of dataframeA subset of specific rows of dataframeB?

Subset rows based on a specific threshold value

subset df by masking between specific rows

Subset specific rows and the following ones in R

Subset specific rows in a dataframe, but keeping the observations

Sorting only specific subset of rows in pandas dataframe

Data table subset using a condition, and only specific rows from that subset

Subset in R does not seem to work : Value of rows with specific values to be subset

Sort pandas df subset of rows (within a group) by specific column

How to subset specific rows of a dataframe conditioned to the datetime of another dataframe

Create a subset of a data.frame by removing specific rows

Subset rows preceding a specific row value in grouped data using R

Subset of a Pandas Dataframe consisting of rows with specific column values

R subset/keep all rows with at least two specific text strings

R: Count number of rows in columns for a specific value, in a data subset

R subset rows that are NAs except where row contains a specific string

How to select a subset of rows based on a specific range in Python

Phpmyadmin- Display A specific category after every four Category rows

Merge rows in tibble

Removing offsetting rows in a tibble

Plotting rows of a Tibble

How to overwrite some rows of a tibble with another tibble

How to add rows to a tibble at a specific place (index) without specifying column names? (R)

How to replace values in specific rows of some columns in R tibble with transformed values conditional on row values?

Sample a specific number of tibble rows conditional to a variable/column reaching a certain mean value

How can I select a specific element across all rows from a list within a tibble in R

Subset tibble (tidyverse) with indexes (short notation)