Chapter 6 Neue Variablen erzeugen

6.1 Base System

Wenn wir auf eine Variable, also auf eine Spalte in einem Dataframe zugreifen wollen, können wir das beispielsweise über den “$” Operator. Das Prinzip: Dataframname$Spaltenname Wenn wir als Spaltennamen einen unbekannten Namen verwenden, wird eine Spalte diesen Namens generiert. Hier können z. B. dann Ergebnisse von Berechnungen aus anderen Variablen gespeichert werden. Durch zuweisen von NA können wir eine neue leere Spalte anlegen.

# we read some data
dd <- readRDS(gzcon(url("http://md.psych.bio.uni-goettingen.de/mv/data/div/stud.rds")))
# which columns do we have
colnames(dd)
##  [1] "no"                    "height"                "shoe_size"             "weight"               
##  [5] "gender"                "birth_month"           "birth_year"            "statistics_grade"     
##  [9] "abitur"                "math_intense"          "academic_background"   "computer_knowledge"   
## [13] "string"                "f_academic_background"
# we might want to have a new column named f_math_intense and in a first step keep it empty
dd$f_math_intense <- NA
head(dd[,10:ncol(dd)])
##   math_intense academic_background computer_knowledge     string f_academic_background f_math_intense
## 1            0                   1                  7 grün                         yes             NA
## 2            0                   1                  5 groß                         yes             NA
## 3            0                   0                  4 öde                           no             NA
## 4            1                   1                  5  süß                         yes             NA
## 5            0                   1                  2 ärgerlich                    yes             NA
## 6            1                   0                  5 Ärger                         no             NA
# we might want to factorise math_intense and store the result in the newly created column
dd$f_math_intense <- factor(dd$math_intense, levels = c(0, 1), labels=c("no", "yes"))
head(dd[,10:ncol(dd)])
##   math_intense academic_background computer_knowledge     string f_academic_background f_math_intense
## 1            0                   1                  7 grün                         yes             no
## 2            0                   1                  5 groß                         yes             no
## 3            0                   0                  4 öde                           no             no
## 4            1                   1                  5  süß                         yes            yes
## 5            0                   1                  2 ärgerlich                    yes             no
## 6            1                   0                  5 Ärger                         no            yes

6.2 Tidyverse: neue Variablen

Zum Erzeugen von neuen Variablen gibt es in Tidyverse dplyr::mutate(). ->' wirkt wie<-` weist also das, woher der “Pfeil” kommt dem zu, wohin der “Pfeil” zeigt. Wenn wir, wie unten, etwas unter einem Namen speichern, den es schon gibt, wird das Objekt diesen Namens überschrieben.

dd <- readRDS(gzcon(url("http://md.psych.bio.uni-goettingen.de/mv/data/div/stud.rds")))
library(tidyverse)
# which columns do we have
colnames(dd)
##  [1] "no"                    "height"                "shoe_size"             "weight"               
##  [5] "gender"                "birth_month"           "birth_year"            "statistics_grade"     
##  [9] "abitur"                "math_intense"          "academic_background"   "computer_knowledge"   
## [13] "string"                "f_academic_background"
# we might want to have a new column named f_math_intense and in a first step keep it empty
dd %>% dplyr::mutate(f_math_intense = NA) -> dd
# head(dd[,10:ncol(dd)])
dd %>% dplyr::filter(no < 6) %>% dplyr::select(math_intense:f_math_intense)
##   math_intense academic_background computer_knowledge     string f_academic_background f_math_intense
## 1            0                   1                  7 grün                         yes             NA
## 2            0                   1                  5 groß                         yes             NA
## 3            0                   0                  4 öde                           no             NA
## 4            1                   1                  5  süß                         yes             NA
## 5            0                   1                  2 ärgerlich                    yes             NA
# we might want to factorise math_intense and store the result in the newly created column
dd %>% dplyr::mutate(f_math_intense = as_factor(math_intense)) %>% 
    dplyr::mutate(f_math_intense = recode_factor(math_intense, "0"="no", "1"="yes")) -> dd
dd %>% dplyr::filter(no < 6) %>% dplyr::select(math_intense:f_math_intense)
##   math_intense academic_background computer_knowledge     string f_academic_background f_math_intense
## 1            0                   1                  7 grün                         yes             no
## 2            0                   1                  5 groß                         yes             no
## 3            0                   0                  4 öde                           no             no
## 4            1                   1                  5  süß                         yes            yes
## 5            0                   1                  2 ärgerlich                    yes             no

6.2.1 A Recommendation

Never ever change values in place. Create a new variable (column) instead.

6.3 Referenzen

6.4 Screencast

… mit Vorstellen einiger Möglichkeiten