Seite 1 von 1

Wenn Spalte A "Wort" enthält, dann Spalte B "Ja"

Verfasst: Do Feb 18, 2021 8:52 pm
von Drako
Hallo zusammen,
leider finde ich in der Suche nichts, weiß aber auch nicht so recht, nach welchen Begriffen ich suchen soll. Ich habe eine Spalte in meiner Tabelle, welche die Ergebnisse einer einzigen Frage mit 4 Checkboxen enthält. Die Spalte sieht so aus:

Code: Alles auswählen

Welche Module nutzen Sie?
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)
Lernmodul;Simulator
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module
Lernmodul;Simulator;Do It Yourself-Module
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module
Lernmodul;Simulator
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)
Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module
Um damit weiter arbeiten zu können, würde ich gerne 4 neue Spalten erzeugen: Eine Spalte pro Antwortmöglichkeit (Lernmodul, Simulator, etc.). In der neuen Spalte "Lernmodul" soll dann in jeder Zeile nur dann ein "Ja" stehen, wenn in der ursprünglichen Spalte auch das Wort "Lernmodul" vorkommt. Ansonsten soll dort "Nein" stehen.
Das Ergebnis soll dann also so wie im Beispiel im Anhang aussehen (ok, da habe ich "Ja" noch mit 1 kodiert...).
Habt ihr eine Idee oder könnt mir sagen, wonach in suchen muss?

Vielen Dank im Voraus!

Re: Wenn Spalte A "Wort" enthält, dann Spalte B "Ja"

Verfasst: Fr Feb 19, 2021 12:14 am
von EDi
vermutlich am einfachsten: 4x grepl anwenden.

Aber es gibt da ein Sprichwort: "Regex: Jetzt hast du 2 Probleme" :lol:

Re: Wenn Spalte A "Wort" enthält, dann Spalte B "Ja"

Verfasst: Fr Feb 19, 2021 8:42 am
von bigben
Nicht alle Probleme mit Regex fallen sofort auf und im Beispiel ist erstmal immer alles einfach :lol:

Code: Alles auswählen

daten <- data.frame(modul = c("Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)", "Lernmodul;Simulator", 
                              "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)", "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module", 
                              "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module", 
                              "Lernmodul;Simulator;Do It Yourself-Module", "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)", 
                              "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module", 
                              "Lernmodul;Simulator", "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module", 
                              "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis)", "Lernmodul;Simulator;Selbsttest (Kenntnisnachweis);Do It Yourself-Module"
))

daten$Selbsttest <- 0
daten$Selbsttest[grep("Selbsttest", daten$modul)] <- 1

daten$DIY <- 0
daten$DIY[grep("Yourself", daten$modul)] <- 1

print(daten)
LG,
Bernhard

Re: Wenn Spalte A "Wort" enthält, dann Spalte B "Ja"

Verfasst: Fr Feb 26, 2021 10:10 pm
von Drako
Hallo ihr beiden,
super, vielen Dank. Das hat hervorragend funktioniert! Die meisten anderen Sachen kann ich mir ergooglen, aber dabei war's echt schwierig!
Nochmals tausend Dank! :)