2017-09-30 3 views
1

Ich versuche, eine neue Spalte zu einem vorhandenen R dataframe hinzuzufügen, der je nach Wert innerhalb des entsprechenden Zeilenwerts eine neue Spalte hinzufügen wird. Wenn der Wert 1 neuen Spaltenwert sollte one enthalten, wenn der Wert 2 neuen Spaltenwert sollte two enthalten, sonst three or moreSpalte mit benutzerdefiniertem Wert zu Datenframe hinzufügen

Dieser Code:

mydf <- data.frame(a = 1:6, 
        b = rep("reproducible", 6), 
        c = rep("example", 6), 
        stringsAsFactors = FALSE) 
mydf 

macht:

enter image description here

Verwendung Code:

mydf["encoded"] <- { if (mydf['a'] == 1) 'one' else if (mydf['a'] == 2) 'two' else 'three or more' } 
mydf 

macht:

enter image description here

Eine Warnung wird auch erzeugt:

Warning message in if (mydf["a"] == 1) "one" else if (mydf["a"] == 2) "two" else "three or more": 
“the condition has length > 1 and only the first element will be used” 

wird eine neue Spalte hinzugefügt dataframe aber alle Werte sind gleich: one

Ich habe nicht die Logik implementiert, um die neuen Spaltenwerte korrekt hinzuzufügen?

Antwort

2

Lösung dplyr::case_when:

Syntax und Logik ist selbsterklärend: wenn a zu 1 gleich - encoded gleich "eins"; wenn a gleich 2 ist - encoded ist gleich "zwei"; alle anderen Fälle - codiert ist gleich "drei oder mehr".
Und mutate erstellt nur eine neue Spalte.

library(dplyr) 
mutate(mydf, encoded = case_when(a == 1 ~ "one", 
           a == 2 ~ "two", 
           TRUE ~ "three or more")) 

    a   b  c  encoded 
1 1 reproducible example   one 
2 2 reproducible example   two 
3 3 reproducible example three or more 
4 4 reproducible example three or more 
5 5 reproducible example three or more 
6 6 reproducible example three or more 

Lösung mit base::ifelse:

mydf$encoded <- ifelse(mydf$a == 1, 
         "one", 
         ifelse(mydf$a == 2, 
           "two", 
           "three or more")) 

Wenn Sie nicht wie mydf$a mehrere Male Schreiben Sie with verwenden:

mydf$encoded <- with(mydf, ifelse(a == 1, 
            "one", 
            ifelse(a == 2, 
             "two", 
             "three or more"))) 
3

Eine viel übersehen Funktion Tun Sie das ist die cut Funktion:

mydf$encoded <- cut(mydf$a, c(0:2,Inf), c('one','two','three or more')) 

Das Ergebnis:

> mydf 
    a   b  c  encoded 
1 1 reproducible example   one 
2 2 reproducible example   two 
3 3 reproducible example three or more 
4 4 reproducible example three or more 
5 5 reproducible example three or more 
6 6 reproducible example three or more 
+1

Das ist besser als die Hand, die Bedingungen zu schreiben :) wie Sie antworten, +1 – Wen

1

sapply auch tun können, um den Job:

mydf$encoded <- sapply(
    mydf$a, function(a) 
     if (a == 1) 'one' else if (a == 2) 'two' else 'three or more') 
mydf 
# a   b  c  encoded 
# 1 1 reproducible example   one 
# 2 2 reproducible example   two 
# 3 3 reproducible example three or more 
# 4 4 reproducible example three or more 
# 5 5 reproducible example three or more 
# 6 6 reproducible example three or more 
Verwandte Themen