R Filterfunktion

Wie rufe ich R-Funktionen auf, wie selektiere ich Daten, ich weiß nicht genau ....

Moderatoren: EDi, jogo

Maya
Beiträge: 6
Registriert: Mi Mär 13, 2019 6:34 pm

R Filterfunktion

Beitrag von Maya »

Hallo liebes R Forum,

ich stehe for ein kleines Problem. Am besten schaut euch den Link an: https://www.bilder-upload.eu/bild-630fc ... 1.jpg.html

Und zwar möchte ich gerne die Filterfunktion in R mit drei Bedinungen nutzen. Es soll einmal in der Spalte race.ethnicity nach "Group C" gefiltert werden und zusätzlich soll nur der Bildungsbschluss "High school" und "bachelor´s degree" angezeigt werden,. Wie genau benkomme ich das hin? Folgenden Code habe ich verwendet. Zusazt: Die Tabelle soll in eine neue Tabelle gespeichert werden > in diesem Fall Students.neu

> Students.neu <- Students %>%
+ filter(parental.level.of.education == "high school", parental.level.of.education == "bachelor´s degree", race.ethnicity == "group C")

>View(Students.neu)

Über eine Rückmeldung /Hilfestellung würde ich mich sehr freuen.

LG

Maya
mara
Beiträge: 4
Registriert: Mo Feb 25, 2019 10:31 am

Re: R Filterfunktion

Beitrag von mara »

Hallo Maya,

was für einen Fehler bekommst du denn genau? So auf den ersten Blick (wenn die Variablennamen alle stimmen, die libraries (wie z.B. magrittr) installiert und geladen sind etc.) müsste doch

Code: Alles auswählen

			filter(Students, parental.level.of.education == "high school", 
			+ parental.level.of.education == "bachelor´s degree",
			+ race.ethnicity == "group C")
das gewünschte Ergebnis bringen? Wenn nicht, poste doch gerne deinen ganzen (in dieser Hinsicht relevanten) Code und vielleicht ein paar Zeilen der Tabelle, damit man das Problem besser nachvollziehen kann.

Viele Grüße, mara
jogo
Beiträge: 2085
Registriert: Fr Okt 07, 2016 8:25 am

Re: R Filterfunktion

Beitrag von jogo »

Hallo Maya,

willkommen im Forum!
Meinst Du im Sinne von:

Code: Alles auswählen

subset(Students,(parental.level.of.education == "high school" | parental.level.of.education == "bachelor´s degree") & race.ethnicity == "group C")
Vielleicht gibt es aber auch ein Problem mit den Datentypen. Bitte kopiere den Output von

Code: Alles auswählen

str(Students)
in Deine nächste Nachricht!

Gruß, Jörg
Maya
Beiträge: 6
Registriert: Mi Mär 13, 2019 6:34 pm

Re: R Filterfunktion

Beitrag von Maya »

Hallo zusammen,

vielen Dank für Ihre schnelle Rückmeldung. Nach der Eigabe von jogo erhalte ich folgendes Ergebnis. Es wird zwar nach group c ausgegeben, aber nur high school und nicht zusätzlich bachelor´s degree. ;)

Code:
> subset(Students,(parental.level.of.education == "high school" | parental.level.of.education == "bachelor´s degree") & race.ethnicity == "group C")

17 male group C high school standard none 88 89 86
29 male group C high school standard none 70 70 65
48 female group C high school standard none 66 71 76
50 male group C high school standard completed 82 84 82
56 female group C high school free/reduced none 33 41 43
92 male group C high school free/reduced none 27 34 36
93 male group C high school standard none 71 79 71
112 male group C high school standard none 62 55 49
116 male group C high school standard none 84 77 74
164 male group C high school standard none 52 53 49
167 male group C high school free/reduced completed 53 51 51
213 female group C high school free/reduced none 42 62 60
218 female group C high school free/reduced none 34 42 39
250 male group C high school standard none 68 60 53
299 male group C high school free/reduced completed 40 46 50
316 male group C high school standard none 71 66 65
321 female group C high school free/reduced completed 67 79 84
325 female group C high school free/reduced none 41 46 43
331 male group C high school standard none 71 60 61
341 male group C high school free/reduced none 58 61 52
342 female group C high school standard none 61 73 63
346 female group C high school standard none 72 80 75
367 male group C high school standard completed 69 58 53
405 female group C high school standard none 54 59 62
432 female group C high school standard none 61 72 70
433 male group C high school standard none 61 56 55
439 male group C high school standard none 70 56 51
448 male group C high school standard completed 86 81 80
472 female group C high school standard none 63 69 74
486 male group C high school standard none 70 74 71
525 male group C high school standard completed 53 52 49
528 female group C high school free/reduced none 36 53 43
541 male group C high school standard completed 75 69 68
548 male group C high school standard completed 72 67 64
549 female group C high school free/reduced none 62 67 64
574 female group C high school free/reduced completed 50 66 64
602 female group C high school standard none 29 29 30
616 female group C high school standard none 60 68 72
621 female group C high school free/reduced none 35 61 54
623 male group C high school free/reduced completed 58 51 52
634 female group C high school standard none 75 88 85
664 female group C high school standard none 65 69 67
671 female group C high school free/reduced none 66 76 68
700 male group C high school free/reduced none 66 66 59
719 female group C high school standard none 76 76 74
724 male group C high school standard none 50 48 42
740 male group C high school free/reduced none 61 60 55
743 female group C high school standard none 81 84 82
761 female group C high school free/reduced none 53 72 64
767 female group C high school standard completed 58 75 77
809 male group C high school standard none 90 75 69
815 female group C high school standard none 72 80 83
826 male group C high school standard none 62 67 58
836 female group C high school standard completed 60 64 74
849 female group C high school standard none 59 72 68
867 male group C high school free/reduced none 59 53 52
888 male group C high school free/reduced none 54 72 59
918 female group C high school standard none 44 61 52
922 female group C high school free/reduced none 35 53 46
943 male group C high school standard none 81 66 64
954 male group C high school standard completed 58 52 54
960 male group C high school standard none 75 81 71
997 male group C high school free/reduced none 62 55 55
998 female group C high school free/reduced completed 59 71 65
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: R Filterfunktion

Beitrag von bigben »

Dazu gehen mir zwei Fragen sofort durch den Kopf: Bist Du sicher, dass es datensätze mit bachelor's degree gibt und vor allem, ist die Schreibweise "bachelor´s degree" richtig? Es gibt ga mindestens drei Möglichkeiten, dieses Hochkomma da zu setzen

Code: Alles auswählen

'  `  ´ 
und wenn das im Auswahlcode anders als in der Datei ist, dann wird die Filterfunktino gnadenlos sein.

Vielleicht postest Du mal das Ergebnis von

Code: Alles auswählen

levels(Students$parental.level.of.education )
und das von

Code: Alles auswählen

table(Students$parental.level.of.education, Students$race.ethnicity)  
JMTC,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
Maya
Beiträge: 6
Registriert: Mi Mär 13, 2019 6:34 pm

Re: R Filterfunktion

Beitrag von Maya »

Code: Alles auswählen

> levels(Students$parental.level.of.education)
[1] "associate's degree" "bachelor's degree"  "c"                  "high school"        "master's degree"    "some college"      
[7] "some high school"  

> table(Students$parental.level.of.education, Students$race.ethnicity)
                    
                     group A group B group C group D group E
  associate's degree      14      41      78      50      39
  bachelor's degree       12      20      40      28      18
  c                        0       1       0       0       0
  high school             18      47      64      44      22
  master's degree          3       6      19      23       8
  some college            18      37      69      67      35
  some high school        24      38      49      50      18
Zusatz: Ich probiere es die ganze Zeit schon mit einem anderen Abschluss "high school", weil ich auch vermutet habe das R Probleme mit dem Semikolon hat.
Ich habe nun folgenden neuen Code angewendet:

Code: Alles auswählen

> Students.neu <- Students %>%
+ filter(parental.level.of.education == "some college" & race.ethnicity == "group C")
> View(Students.neu)
Jetzt wird mir aktuell alle "some college" und alle „group C“ angezeigt. Ich bin schon mal ein Schritt weiter :)
Aber wie bekomme ich es jetzt noch hin einen weiteren Abschluss anzeigen zu lassen?


Moderatoreingriff: code-Tags ergänzt, bigben
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: R Filterfunktion

Beitrag von bigben »

Da "bachelor's degree" der zweite vorkommende Level ist sollte ... == "bachelor's degree" eigentlich identisch sein mit == 2.

Aus dem Table-Befund kann man ersehen, wieviele Menschen mit welchem Bildungsabschluss in welcher Gruppe sind. Wie man so eine Tabelle im Forum lesbar darstellt, kannst Du hier nachlesen: viewtopic.php?f=20&t=29

LG,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
Maya
Beiträge: 6
Registriert: Mi Mär 13, 2019 6:34 pm

Re: R Filterfunktion

Beitrag von Maya »

Students.csv
Students
(71.32 KiB) 40-mal heruntergeladen
Hallo Bernhard,

das kann doch eigentlich nicht so kompliziert sein :roll:
Es muss doch eine Möglichkeit geben in der Spalte auf zwei Filterfunktionen zurückzugreifen. Ich habe die Datei mal im Anhang angehängt. Vielleicht kann man so es besser nachvollziehen ;)

Folgende Darstellung würde ich beispielsweise ausgegeben bekommen:

male group C high school standard none 88 89 86
male group C standard none 70 70 65
female group C high school standard none 66 71 76
male group C bachelor´s degree standard completed 82 84 82
female group C bachelor´s degree/reduced none 33 41 43
male group C high school free/reduced none 27 34

Nach "Group C" und "high school" kann ich wie gesagt schon filtern,
Students.csv
Students
(71.32 KiB) 40-mal heruntergeladen
aber ich würde gerne noch weitere Bildungsabschlüsse ausgegeben bekommen.


Um weitere Unterstützung wäre ich sehr dankbar :)
jogo
Beiträge: 2085
Registriert: Fr Okt 07, 2016 8:25 am

Re: R Filterfunktion

Beitrag von jogo »

Hallo Maya,

Code: Alles auswählen

Students <- read.csv("http://forum.r-statistik.de/download/file.php?id=457")
subset(Students,(parental.level.of.education == "high school" | parental.level.of.education == "bachelor's degree") & race.ethnicity == "group C")
oder

Code: Alles auswählen

subset(Students,(parental.level.of.education %in% c("high school", "bachelor's degree")) & race.ethnicity == "group C")

Gruß, Jörg
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: R Filterfunktion

Beitrag von bigben »

Probier das mal und sag, was daran noch nicht passt:

Code: Alles auswählen

students <- read.csv("http://forum.r-statistik.de/download/file.php?id=457")

nurC <- subset(students, race.ethnicity =="group C")
auswahl <- subset(nurC, parental.level.of.education %in% 
                    c("bachelor's degree", "high school"))

print(head(auswahl, 10))
anstelle von

Code: Alles auswählen

 c("bachelor's degree", "high school")
passt halt auch jede Kombination von

Code: Alles auswählen

c("associate's degree", "bachelor's degree",  "c", 
"high school", "master's degree", "some college",
"some high school")
wobei das nicht abtippen sondern mit copy&paste übernehmen solltest.

LG,
Bernhard


Edit: Jörg war mal wieder schneller :D
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
Antworten