Kapitel 63 Suchen nach Variablen
Das Suchen nach Variablen ist manchmal wie das Suchen nach der Nadel im Heuhaufen. Doch nicht mehr mit dem lookfor Befehl aus dem questionr Paket.
63.1 Lookfor
Das R Paket questionr bietet eine sehr hilfreiche Funktion, den Befehl lookfor.
Mit lookfor(data, “Teil_des_Names_der_Variable”) können wir im data frame data nach Variablen suchen, die “Teil_des_Names_der_Variable” im Variablen-Namen haben.
Erstellen wir zuerst ein paar Daten für ein Beispiel:
library(questionr)
Thats_a_cool_variable <-rnorm(100, 50,12)
Thats_a_less_cool_variable <-rnorm(100,50,12)
Thats_an_even_cooler_variable<-rnorm(100,40,21)
Alter<-rnorm(100, 50,4)
Altersklassen<-cut(Alter, breaks=c(18,30,40,50,60,70,75,80)) # either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut
Does_R_makes_you_angry=rep(0:1,each=50)
Does_R_makes_you_happy=rep(c(1,0),each=50)
data<-data.frame(Thats_an_even_cooler_variable, Thats_a_cool_variable, Thats_a_less_cool_variable, Does_R_makes_you_angry, Does_R_makes_you_happy, Alter, Altersklassen)
names(data)## [1] "Thats_an_even_cooler_variable" "Thats_a_cool_variable"
## [3] "Thats_a_less_cool_variable" "Does_R_makes_you_angry"
## [5] "Does_R_makes_you_happy" "Alter"
## [7] "Altersklassen"
In diesem Beispiel werden wir nichts finden.
# Example that will yield nothing:
lookfor(data, "coll")## Nothing found. Sorry.
Mit diesem Beispiel werden wir etwas finden.
# Example that will find something:
lookfor(data, "cool")## pos variable label col_type values
## 1 Thats_an_even_cooler_variable — dbl
## 2 Thats_a_cool_variable — dbl
## 3 Thats_a_less_cool_variable — dbl
Noch ein Beispiel:
lookfor(data, "Alter")## pos variable label col_type values
## 6 Alter — dbl
## 7 Altersklassen — fct (18,30]
## (30,40]
## (40,50]
## (50,60]
## (60,70]
## (70,75]
## (75,80]
Oder:
lookfor(data, "Alt")## pos variable label col_type values
## 6 Alter — dbl
## 7 Altersklassen — fct (18,30]
## (30,40]
## (40,50]
## (50,60]
## (60,70]
## (70,75]
## (75,80]
Oder:
lookfor(data, "Alters")## pos variable label col_type values
## 7 Altersklassen — fct (18,30]
## (30,40]
## (40,50]
## (50,60]
## (60,70]
## (70,75]
## (75,80]
Ihr werdet sehen, vor allem bei grossen Datentabellen ist diese Funktion extrem hilfreich.
Wer suchet der findet.