Subsetting data with dplyr¶
The following makes use of the dplyr package. You may need to install it
from CRAN using the code
install.packages("dplyr")
if you want to run this on your computer. (The package is already installed on
the notebook container, however.)
Here we use data from the British Election Study 2010. The data set bes2010feelings.RData is prepared from the original available at https://www.britishelectionstudy.com/data-object/2010-bes-cross-section/ by removing identifying information and scrambling the data.
load("bes2010feelings.RData")
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
bes2010feelings.sub <- bes2010feelings %>%
filter(region == "Scotland") %>%
select(wave,
flng.brown,
flng.cameron,
flng.clegg,
flng.salmond)
head(bes2010feelings.sub)
wave flng.brown flng.cameron flng.clegg flng.salmond
55002.1 Pre 8 3 NA 7
55003.1 Pre 0 3 NA 2
55006.1 Pre 4 5 5 7
55007.1 Pre 6 5 6 6
55010.1 Pre 8 6 7 0
55015.1 Pre 9 3 3 4
- R file: subsetting-data-with-dplyr.R
- Rmarkdown file: subsetting-data-with-dplyr.Rmd
- Jupyter notebook file: subsetting-data-with-dplyr.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):