Accessing and changing variables¶
Here we use data from the British Election Study 2010. The data set bes2010feelings-prepost.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-prepost.RData")
with()
versus attach()
¶
c(
Brown = mean(bes2010flngs_pre$flng.brown,na.rm=TRUE),
Cameron = mean(bes2010flngs_pre$flng.cameron,na.rm=TRUE),
Clegg = mean(bes2010flngs_pre$flng.clegg,na.rm=TRUE),
Salmond = mean(bes2010flngs_pre$flng.salmond,na.rm=TRUE),
Jones = mean(bes2010flngs_pre$flng.jones,na.rm=TRUE)
)
Brown Cameron Clegg Salmond Jones
4.339703 5.090708 4.557366 4.505660 4.235949
## Here we define a convenience function.
Mean <- function(x,...) mean(x,na.rm=TRUE,...)
## Use of 'attach'
# The following code shows how the use of 'attach' can lead to confusion
attach(bes2010flngs_pre)
c(
Brown = Mean(flng.brown),
Cameron = Mean(flng.cameron),
Clegg = Mean(flng.clegg),
Salmond = Mean(flng.salmond),
Jones = Mean(flng.jones)
)
Brown Cameron Clegg Salmond Jones
4.339703 5.090708 4.557366 4.505660 4.235949
attach(bes2010flngs_post)
c(
Brown = Mean(flng.brown),
Cameron = Mean(flng.cameron),
Clegg = Mean(flng.clegg),
Salmond = Mean(flng.salmond),
Jones = Mean(flng.jones)
)
The following objects are masked from bes2010flngs_pre:
flng.bnp, flng.brown, flng.cameron, flng.clegg, flng.cons,
flng.green, flng.jones, flng.labour, flng.libdem, flng.pcym,
flng.salmond, flng.snp, flng.ukip, region
Brown Cameron Clegg Salmond Jones
4.448116 5.206120 5.001756 4.228707 4.509317
detach(bes2010flngs_post)
c(
Brown = Mean(flng.brown),
Cameron = Mean(flng.cameron),
Clegg = Mean(flng.clegg),
Salmond = Mean(flng.salmond),
Jones = Mean(flng.jones)
)
Brown Cameron Clegg Salmond Jones
4.339703 5.090708 4.557366 4.505660 4.235949
detach(bes2010flngs_pre)
# 'with()' is a better alternative, because it is clear where the data in the varialbes come from:
with(bes2010flngs_pre,c(
Brown = Mean(flng.brown),
Cameron = Mean(flng.cameron),
Clegg = Mean(flng.clegg),
Salmond = Mean(flng.salmond),
Jones = Mean(flng.jones)
))
Brown Cameron Clegg Salmond Jones
4.339703 5.090708 4.557366 4.505660 4.235949
with(bes2010flngs_post,c(
Brown = Mean(flng.brown),
Cameron = Mean(flng.cameron),
Clegg = Mean(flng.clegg),
Salmond = Mean(flng.salmond),
Jones = Mean(flng.jones)
))
Brown Cameron Clegg Salmond Jones
4.448116 5.206120 5.001756 4.228707 4.509317
Changing variables within a data frame¶
bes2010flngs_pre <- within(bes2010flngs_pre,{
ave_flng <- (flng.brown + flng.cameron + flng.clegg)/3
rel_flng.brown <- flng.brown - ave_flng
rel_flng.cameron <- flng.cameron - ave_flng
rel_flng.clegg <- flng.clegg - ave_flng
})
with(bes2010flngs_pre,c(
Brown = Mean(rel_flng.brown),
Cameron = Mean(rel_flng.cameron),
Clegg = Mean(rel_flng.clegg)
))
Brown Cameron Clegg
-0.3960328 0.5068399 -0.1108071
# It is also possible without 'within()' but this is terribly tedious:
bes2010flngs_pre$ave_flng <- (bes2010flngs_pre$flng.brown +
bes2010flngs_pre$flng.cameron +
bes2010flngs_pre$flng.clegg)/3
bes2010flngs_pre$rel_flng.brown <- (bes2010flngs_pre$flng.brown
- bes2010flngs_pre$ave_flng)
bes2010flngs_pre$rel_flng.cameron <- (bes2010flngs_pre$flng.cameron
- bes2010flngs_pre$ave_flng)
bes2010flngs_pre$rel_flng.clegg <- (bes2010flngs_pre$flng.clegg
- bes2010flngs_pre$ave_flng)
with(bes2010flngs_pre,c(
Brown = Mean(rel_flng.brown),
Cameron = Mean(rel_flng.cameron),
Clegg = Mean(rel_flng.clegg)
))
Brown Cameron Clegg
-0.3960328 0.5068399 -0.1108071
Downloadable R script and interactive version
- R Script: accessing-and-changing-variables.R
- Interactive version (shuts down after 60s):
- Interactive version (sign in required):
Explanation
The link with the “jupyterhub” icon directs you to an interactive Jupyter1 notebook, which runs inside a Docker container2. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID3 credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.4
Above you see a rendered version of the Jupyter notebook.5
- 1
-
For more information about Jupyter see
http://jupyter.org
. The Jupyter notebooks make use of the IRKernel package. - 2
-
For more information about Docker see
https://docs.docker.com/
. The container images were created with repo2docker, while containers are run with docker spawner. - 3
-
ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See
https://info.orcid.org/what-is-orcid/
for more information. - 4
-
The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.
- 5
-
The notebook is rendered with the help of the nbsphinx extension.