2017-10-02 2 views
0

Ich habe ein ziemlich interessantes Problem, dass ich eine intelligente und mögliche effiziente Lösung für suchen.R "explodieren" Spalten eines Datenrahmens

Ich habe einen Datenrahmen, der so aussieht.

# A tibble: 6 x 6 
    track_id tag1 tag2 tag3 tag4 tag5 
    <int> <dbl> <dbl> <dbl> <dbl> <dbl> 
1 1550729 54087 109806 46869 183258 54337 
2 1184201 201327 3668 46208 205245 189631 
3 3763749 194264 194413 3424 91383 205245 
4 2674608 198998 107401 2327 4425 107398 
5 1999180 54087 4425 75574 239459 2397 
6 3048820 11242 205245 2474 11056 72354 

Was Ich mag würde, ist die track_id in der ersten Reihe zu halten, aber die Tags zu explodieren, und wo eine Spur die bestimmte ID hat, würde Ich mag einen wahren Wert setzen, nämlich ein.

Um klarer, nehmen wir an, ich von einem Start kleinere:

track_id tag1 tag2 
1 1550729 54087 109806 
2 1184201 201327 3668 

Nach der Umwandlung in etwas bekommen, wie ich möchte

track_id 54087 109806 201327 3668 
1 1550729  1  1  0  0 
2 1184201  0  0  1  1 

Ist das etwas schnell möglich oder ich sollte eine Lösung von Hand ausrollen?

+0

Suche nach "Dummy-Variable" und/oder „ein- hot encoding " –

+0

Zuerst konvertieren Sie die' tag' Variablen in Faktoren und dann 'model.matrix (~. + 0, data = your_data_frame)'. Vorgeschlagene Duplikate: https://stackoverflow.com/q/11952706/903061, https://stackoverflow.com/q/24142576/903061 – Gregor

Antwort

6

Eine Lösung von dplyr und tidyr.

library(dplyr) 
library(tidyr) 

dt2 <- dt %>% 
    gather(tag, value, -track_id) %>% 
    select(-tag) %>% 
    mutate(Occurrence = 1) %>% 
    spread(value, Occurrence, fill = 0) 

DATA

dt <- read.table(text = " track_id tag1 tag2 tag3 tag4 tag5 
1 1550729 54087 109806 46869 183258 54337 
2 1184201 201327 3668 46208 205245 189631 
3 3763749 194264 194413 3424 91383 205245 
4 2674608 198998 107401 2327 4425 107398 
5 1999180 54087 4425 75574 239459 2397 
6 3048820 11242 205245 2474 11056 72354", 
       header = TRUE) 
3

Vielleicht so etwas wie die folgenden.

dat <- read.table(text = " 
track_id tag1 tag2 
1 1550729 54087 109806 
2 1184201 201327 3668 
", header = TRUE) 
dat 

molten <- reshape2::melt(dat, id.vars = "track_id") 
xtabs(~ track_id + value, molten) 
#   value 
#track_id 3668 54087 109806 201327 
# 1184201 1  0  0  1 
# 1550729 0  1  1  0 
4

Mit melt() und dcast() vom data.table Paket, wird dies zu einem "one-liner":

library(data.table) 
melt(setDT(df), id.vars = "track_id")[, dcast(.SD, track_id ~ value, length)] 
track_id 2327 2397 2474 3424 3668 4425 11056 11242 46208 46869 54087 54337 72354 75574 
1: 1184201 0 0 0 0 1 0  0  0  1  0  0  0  0  0 
2: 1550729 0 0 0 0 0 0  0  0  0  1  1  1  0  0 
3: 1999180 0 1 0 0 0 1  0  0  0  0  1  0  0  1 
4: 2674608 1 0 0 0 0 1  0  0  0  0  0  0  0  0 
5: 3048820 0 0 1 0 0 0  1  1  0  0  0  0  1  0 
6: 3763749 0 0 0 1 0 0  0  0  0  0  0  0  0  0 
    91383 107398 107401 109806 183258 189631 194264 194413 198998 201327 205245 239459 
1:  0  0  0  0  0  1  0  0  0  1  1  0 
2:  0  0  0  1  1  0  0  0  0  0  0  0 
3:  0  0  0  0  0  0  0  0  0  0  0  1 
4:  0  1  1  0  0  0  0  0  1  0  0  0 
5:  0  0  0  0  0  0  0  0  0  0  1  0 
6:  1  0  0  0  0  0  1  1  0  0  1  0 
Verwandte Themen