[R] data.frame manipulation: Unbinding strings in a row

jim holtman jholtman at gmail.com
Thu Jan 10 13:42:09 CET 2008


here is a quick hack:

> x <- read.table(textConnection("ID      Shop    Items
+ ID1     A1      item1,item2,item3
+ ID2     A2      item4,item5
+ ID3     A1      item1,item3,item4"), header=TRUE)
> y <- lapply(1:nrow(x), function(.row){
+     .items <- strsplit(as.character(x$Items[.row]), ',')[[1]]
+     data.frame(ID=rep(x$ID[.row], length(.items)),
Shop=rep(x$Shop[.row], length(.items)),
+         Item=.items)
+ })
> do.call(rbind,y)
   ID Shop  Item
1 ID1   A1 item1
2 ID1   A1 item2
3 ID1   A1 item3
4 ID2   A2 item4
5 ID2   A2 item5
6 ID3   A1 item1
7 ID3   A1 item3
8 ID3   A1 item4


On Jan 10, 2008 6:40 AM, francogrex <francogrex at mail.com> wrote:
>
> Hi all,
>
> I have a data.frame I received with data that look like this (comma
> separated strings in last row):
>
> ID      Shop    Items
> ID1     A1      item1, item2, item3
> ID2     A2      item4, item5
> ID3     A1      item1, item3, item4
>
>
> But I would like to unbind the strings in col(2) items so that it will look
> like this:
>
> ID      Shop    Items
> ID1     A1      item1
> ID1     A1      item2
> ID1     A1      item3
> ID2     A2      item4
> ID2     A2      item5
> ID3     A1      item1
> ID3     A1      item3
> ID3     A1      item4
>
> Meaning each item is on a different row but still maintain the ties with the
> IDs and the Shops.
> The final purpose is to count how many times a particular item has been
> bought in a particular shop, like:
>
> item1-A1= 2
> item2-A1=1
> item3-A1=2
>
>
> Any ideas? Thanks
>
>
> --
> View this message in context: http://www.nabble.com/data.frame-manipulation%3A-Unbinding-strings-in-a-row-tp14731173p14731173.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?




More information about the R-help mailing list