Factors¶
set.seed(42)
satisfaction <- sample(1:4,size=20,replace=TRUE)
satisfaction
[1] 1 1 1 1 2 4 2 2 1 4 3 4 3 4 1 1 2 4 2 2
satisfaction <- ordered(satisfaction,
levels=1:4,
labels=c(
"not at all",
"low",
"medium",
"high"))
satisfaction
[1] not at all not at all not at all not at all low high
[7] low low not at all high medium high
[13] medium high not at all not at all low high
[19] low low
Levels: not at all < low < medium < high
table(satisfaction)
satisfaction
not at all low medium high
7 6 2 5
levels(satisfaction)
[1] "not at all" "low" "medium" "high"
country.orig <- sample(
c("England","Northern Ireland","Scotland","Wales"),
size=50,
prob=c(54786300,5373000,3099100,1851600)/65110000,
replace=TRUE
)
country <- factor(country.orig)
country
[1] Northern Ireland England Wales Scotland
[5] England England England Northern Ireland
[9] England England England England
[13] England England England England
[17] England England Northern Ireland England
[21] England England England Wales
[25] England Scotland Northern Ireland England
[29] Scotland England England England
[33] England England England England
[37] England England England England
[41] England Wales England England
[45] Northern Ireland England England England
[49] England England
Levels: England Northern Ireland Scotland Wales
country <- factor(country.orig,
levels=c("England","Wales","Scotland",
"Northern Ireland"))
country
[1] Northern Ireland England Wales Scotland
[5] England England England Northern Ireland
[9] England England England England
[13] England England England England
[17] England England Northern Ireland England
[21] England England England Wales
[25] England Scotland Northern Ireland England
[29] Scotland England England England
[33] England England England England
[37] England England England England
[41] England Wales England England
[45] Northern Ireland England England England
[49] England England
Levels: England Wales Scotland Northern Ireland
table(country)
country
England Wales Scotland Northern Ireland
39 3 3 5
str(country)
Factor w/ 4 levels "England","Wales",..: 4 1 2 3 1 1 1 4 1 1 ...
as.numeric(country)
[1] 4 1 2 3 1 1 1 4 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 2 1 3 4 1 3 1 1 1 1 1 1 1 1 1
[39] 1 1 1 2 1 1 4 1 1 1 1 1
levels(country) <- c("EN","NI","SC","WL")
table(country)
country
EN NI SC WL
39 3 3 5
Downloadable R script and interactive version
- R Script: factors.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.