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.
Der Titel die Frage nicht übereinstimmt. Sie fragen nicht nach neuen Variablen zu erstellen, durch Zuweisung basierend auf Regex. – user890739
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). –
@RYoda, ok! Ich füge R-Code hinzu, um einen Datenrahmen zu erstellen. – Denis