2017-03-19 2 views
1

Ich habe einen großen Datenrahmen, der eine Beschreibung der Waren (ca. 11000 Zeilen) enthält. Ich möchte neue Variablen (Produkttyp und Produktfarbe) aus der Goods.Description extrahieren.IF ... ELSE-Anweisung in R für Textanalyse

b <- data.frame(id = c('1','2', '3', '4'), Goods.Description = c("This green T-shirt can become...", "Stripes of unfaded denim at each side of this blue skirt make...", "Velvet's Brynna red top comes in a bohemian...", "The Riley blue jeans are Paige's take on..."), Jeans = c(0,0,0,0), T.Shirt = c(0,0,0,0), Skirt = c(0,0,0,0), Top = c(0,0,0,0), Color = c(0,0,0,0)) 

Datenrahmen:

id            Goods.Description Jeans T.Shirt Skirt Top Color 
1 1         This green T-shirt can become...  0  0  0 0  0 
2 2 Stripes of unfaded denim at each side of this blue skirt make...  0  0  0 0  0 
3 3     Velvet's Brynna red top comes in a bohemian...  0  0  0 0  0 
4 4      The Riley blue jeans are Paige's take on...  0  0  0 0  0 

Zum Beispiel Wenn Goods.Description das Wort "T-Shirt" enthält, dann 1 in T.Shirt setzen, sonst 0.

Wenn Goods.Description das Wort enthält " Jeans ", dann setzen Sie 1 in Jeans, sonst 0.

Wenn Goods.Description das Wort "Rock" enthält, dann 1 setzte in Skirt, sonst 0.

Wenn Goods.Description das Wort "top" enthält, legte 1 in Top dann, sonst 0.

Wenn Goods.Description das Wort "grün" enthält , legte dann green in Color, sonst 0.

Wenn Goods.Description das Wort "blau" enthält, dann blue in Color setzen, sonst 0.

und so weiter

Nach:

id            Goods.Description Jeans T.Shirt Skirt Top Color 
1 1         This green T-shirt can become...  0  1  0 0 green 
2 2 Stripes of unfaded denim at each side of this blue skirt make...  0  0  1 0 blue 
3 3     Velvet's Brynna red top comes in a bohemian...  0  0  0 1 red 
4 4      The Riley blue jeans are Paige's take on...  1  0  0 0 blue 

Ich weiß nicht, was soll der Code sein. Bitte hilf mir.

+0

Der Titel die Frage nicht übereinstimmt. Sie fragen nicht nach neuen Variablen zu erstellen, durch Zuweisung basierend auf Regex. – user890739

+0

Was hast du bisher versucht? Fügen Sie ein minimales reproduzierbares Beispiel als Code hinzu (enthält auch R-Code, um die Datentabelle mit Ihren Beispieldaten zu erstellen, um Zeit zu sparen, um eine Antwort zu liefern). –

+1

@RYoda, ok! Ich füge R-Code hinzu, um einen Datenrahmen zu erstellen. – Denis

Antwort

2
library(data.table) 

b <- data.frame(id = c('1','2', '3', '4'), Goods.Description = c("This green T-shirt can become...", "Stripes of unfaded denim at each side of this blue skirt make...", "Velvet's Brynna red top comes in a bohemian...", "The Riley blue jeans are Paige's take on..."), Jeans = c(0,0,0,0), T.Shirt = c(0,0,0,0), Skirt = c(0,0,0,0), Top = c(0,0,0,0), Color = c(0,0,0,0)) 
str(b) 

setDT(b) # convert to data.table for better performance... 

b[, Jeans := as.integer(grepl("jeans", Goods.Description, fixed = TRUE))] 
b[, Skirt := as.integer(grepl("skirt", Goods.Description, fixed = TRUE))] 
# etc. for each keyword 

# Collect the colors in the "Color" target column 

# initialize with empty string instead of zero (implicitly converting the col class to character) 
b[, Color := NULL] 
b[, Color := ""] 
for (a.color in c("red", "green", "blue", "yellow")) 
    b[grepl(a.color, Goods.Description, fixed = TRUE), Color := paste(Color, a.color)] # paste color names to keep all colors 

b 

Ergebnisse in

Extrahieren
id            Goods.Description Jeans T.Shirt Skirt Top Color 
1: 1         This green T-shirt can become...  0  0  0 0 green 
2: 2 Stripes of unfaded denim at each side of this blue skirt make...  0  0  1 0 blue 
3: 3     Velvet's Brynna red top comes in a bohemian...  0  0  0 0 red 
4: 4      The Riley blue jeans are Paige's take on...  1  0  0 0 blue 
+0

Da Sie for-Schleife verwendet haben, wäre es aus Gründen der Effizienz natürlich, set() zu verwenden, um Werte für die Spalte unter Iteration zu aktualisieren. Bitte lesen? ': =' Für weitere Informationen. – Sathish

+0

wow! Es klappt! Danke) – Denis

+0

@Sathish Thx für den Tipp! Ehrlich gesagt habe ich nie verstanden, wann 'set' die richtige Lösung ist, um es schneller zu machen. Die Hilfe sagt * "[vermeidet] Overhead, um die Existenz und den Typ der Argumente zu überprüfen" *, aber da ich nur ein paar Spalten in dieser Lösung wiederhole (und nicht über alle Zeilen, die viel normaler sind), kann ich 'set nicht erkennen 'könnte hier viel Zeit sparen. –

2

Wir können dies tun, indem Sie die ‚Farbe‘ und spezifische Wörter aus den Spaltennamen

library(stringr) 
b$Color <- str_extract(b$Goods.Description, 'green|blue|red|blue') 
v1 <- toupper(sub(".", "-", names(b)[3:6], fixed = TRUE)) 
b[3:6][cbind(1:nrow(b), match(v1, 
     str_extract(toupper(b$Goods.Description), paste(v1, collapse="|"))))] <- 1 

b 
# id            Goods.Description Jeans T.Shirt Skirt Top Color 
#1 1         This green T-shirt can become...  0  0  0 1 green 
#2 2 Stripes of unfaded denim at each side of this blue skirt make...  1  0  0 0 blue 
#3 3     Velvet's Brynna red top comes in a bohemian...  0  1  0 0 red 
#4 4      The Riley blue jeans are Paige's take on...  0  0  1 0 blue 
Verwandte Themen