2016-06-17 3 views
12

Ich habe eine Tabelle mit Daten von Filmen, und in der letzten Spalte hat es die Kategorien, die der Film gehört.R- Konvertieren Spalte von Listen in verschiedene Spalten mit ihren Werten als Namen (Dummy)

movieId        title     category 
     1     Toy Story (1995) Animation|Children|Comedy 
     2      Jumanji (1995) Adventure|Children|Fantasy 
     3   Grumpier Old Men (1995)    Comedy|Romance 
     4   Waiting to Exhale (1995)    Comedy|Drama 
     5 Father of the Bride Part II (1995)      Comedy 
     6      Heat (1995)  Action|Crime|Thriller 

Ich mag eine Spalte für jede Kategorie erstellen und 1 setzen, wenn es für diesen Film in der Liste geschrieben wurde und Null, wenn nicht. Etwas wie:

movieId title animation comedy drama 
1  xx  1   0  1 
2  xy  1   0  0 
3  yy  1   1  0 

Bisher habe ich nur mit dem String in eine Liste umgewandelt:

f<-function(x) {strsplit(x, split='|', fixed=TRUE)} 
movies2$m<-lapply(movies2$category, f) 

Aber ich weiß nicht, wie Sie den Rest zu tun.

Ich dachte an Python-Wörterbücher. Aber ich weiß nicht, wie diese

Daten in R. tun

df1 <- read.table(header = TRUE, stringsAsFactors = FALSE, 
        text = " movieId        title     category 
        1     'Toy Story (1995)' Animation|Children|Comedy 
        2      'Jumanji (1995)' Adventure|Children|Fantasy 
        3   'Grumpier Old Men (1995)'    Comedy|Romance 
        4   'Waiting to Exhale (1995)'    Comedy|Drama 
        5 'Father of the Bride Part II (1995)'      Comedy 
        6      'Heat (1995)'  Action|Crime|Thriller") 

Antwort

5

Wir mtabulate von qdapTools nach Spaltung können

library(qdapTools) 
cbind(df1[-3],mtabulate(strsplit(df1$category, "[|]"))) 
# movieId        title Action Adventure Animation Children Comedy Crime Drama Fantasy Romance Thriller 
#1  1     Toy Story (1995)  0   0   1  1  1  0  0  0  0  0 
#2  2      Jumanji (1995)  0   1   0  1  0  0  0  1  0  0 
#3  3   Grumpier Old Men (1995)  0   0   0  0  1  0  0  0  1  0 
#4  4   Waiting to Exhale (1995)  0   0   0  0  1  0  1  0  0  0 
#5  5 Father of the Bride Part II (1995)  0   0   0  0  1  0  0  0  0  0 
#6  6      Heat (1995)  1   0   0  0  0  1  0  0  0  1 

Oder mit base R

cbind(df1[-3], as.data.frame.matrix(table(stack(setNames(strsplit(df1$category, 
          "[|]"), df1$movieId))[2:1]))) 
4

Hier ist eine Basis-R-Möglichkeit, die strsplit() verwendet, um die Spaltenwerte aufzuteilen, dann grepl(), um sie in vapply() zu vergleichen. Der Trick ist hier, FUN.VALUE = integer(.) in vapply() zu verwenden, so dass das grepl() Ergebnis magisch in Ganzzahl konvertiert wird.

## split the 'category' column on '|' 
s <- strsplit(df$category, "|", fixed = TRUE) 
## run the unique sorted values through grepl(), getting integer result 
newPart <- vapply(sort(unique(unlist(s))), grepl, integer(nrow(df)), df$category, fixed = TRUE) 
## bind result to other columns 
cbind(df[-3], newPart) 

Dies ergibt den folgenden Datenrahmen.

movieId        title Action Adventure Animation Children Comedy Crime Drama Fantasy Romance Thriller 
1  1     Toy Story (1995)  0   0   1  1  1  0  0  0  0  0 
2  2      Jumanji (1995)  0   1   0  1  0  0  0  1  0  0 
3  3   Grumpier Old Men (1995)  0   0   0  0  1  0  0  0  1  0 
4  4   Waiting to Exhale (1995)  0   0   0  0  1  0  1  0  0  0 
5  5 Father of the Bride Part II (1995)  0   0   0  0  1  0  0  0  0  0 
6  6      Heat (1995)  1   0   0  0  0  1  0  0  0  1 
3

A hadleyverse Ansatz:

library(dplyr) 
library(tidyr) 
library(reshape2) 
library(stringr) 

max.categories = max(str_count(df1$category, "\\|")) + 1 

df1new = df1 %>% separate(category, into=letters[1:max.categories], sep="\\|") %>% 
    melt(c("movieId","title")) %>% 
    filter(!is.na(value)) %>% 
    dcast(movieId + title ~ value, fun.aggregate=length) 
movieId        title Action Adventure Animation Children Comedy Crime Drama Fantasy Romance Thriller 
1  1     Toy Story (1995)  0   0   1  1  1  0  0  0  0  0 
2  2      Jumanji (1995)  0   1   0  1  0  0  0  1  0  0 
3  3   Grumpier Old Men (1995)  0   0   0  0  1  0  0  0  1  0 
4  4   Waiting to Exhale (1995)  0   0   0  0  1  0  1  0  0  0 
5  5 Father of the Bride Part II (1995)  0   0   0  0  1  0  0  0  0  0 
6  6      Heat (1995)  1   0   0  0  0  1  0  0  0  1 

max.categories ist nur ein Weg, um programmatisch sicherzustellen, dass der into Vektor zumindest so lange, wie die maximale Anzahl ist von Kategorien für eine gegebene title. Wenn Sie bereits wissen, dass dieser Wert nie größer als beispielsweise 5 ist, können Sie beispielsweise einfach into=letters[1:5] eingeben.

Verwandte Themen