r - prepare an arules transaction list -
arules
requires list of transactions. each row in list contain array of products. not every transaction has same amount of products. sounds pivot it's not. example can found here
i want aggregate(dvd , by=list("id"), fun=c)
fail arguments must have same length
this data
> dvd id item 1 1 sixth sense 2 1 lotr1 3 1 harry potter1 4 1 green mile 5 1 lotr2 6 2 gladiator 7 2 patriot 8 2 braveheart 9 3 lotr1 10 3 lotr2 11 4 gladiator 12 4 patriot 13 4 sixth sense 14 5 gladiator 15 5 patriot 16 5 sixth sense 17 6 gladiator 18 6 patriot 19 6 sixth sense 20 7 harry potter1 21 7 harry potter2 22 8 gladiator 23 8 patriot 24 9 gladiator 25 9 patriot 26 9 sixth sense 27 10 sixth sense 28 10 lotr 29 10 galdiator 30 10 green mile
i need list looks
tr1 c("sixth sense","lotr1","harry potter1","green mile","lotr2") tr2 c("gladiator","patriot","braveheart") tr3 c("lotr1","lotr2") ....
i think split
job you.
df <- structure(list(id = c(1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 4l, 4l, 4l, 5l, 5l, 5l, 6l, 6l, 6l, 7l, 7l, 8l, 8l, 9l, 9l, 9l, 10l, 10l, 10l, 10l), item = c(" sixth sense", " lotr1", " harry potter1", " green mile", " lotr2", " gladiator", " patriot", " braveheart", " lotr1", " lotr2", " gladiator", " patriot", " sixth sense", " gladiator", " patriot", " sixth sense", " gladiator", " patriot", " sixth sense", " harry potter1", " harry potter2", " gladiator", " patriot", " gladiator", " patriot", " sixth sense", " sixth sense", " lotr", " galdiator", " green mile" )), .names = c("id", "item"), class = "data.frame", row.names = c(na, -30l)) df <- read.csv(textconnection(txt), header = true, stringsasfactors = false, strip.white = true) result <- split(df$item, df$id) names(result) <- gsub("(.*)", "tr\\1", names(result)) result ## $tr1 ## [1] "sixth sense" "lotr1" "harry potter1" "green mile" "lotr2" ## ## $tr2 ## [1] "gladiator" "patriot" "braveheart" ## ## $tr3 ## [1] "lotr1" "lotr2" ## ## $tr4 ## [1] "gladiator" "patriot" "sixth sense" ## ## $tr5 ## [1] "gladiator" "patriot" "sixth sense" ## ## $tr6 ## [1] "gladiator" "patriot" "sixth sense" ## ## $tr7 ## [1] "harry potter1" "harry potter2" ## ## $tr8 ## [1] "gladiator" "patriot" ## ## $tr9 ## [1] "gladiator" "patriot" "sixth sense" ## ## $tr10 ## [1] "sixth sense" "lotr" "galdiator" "green mile"
Comments
Post a Comment