[R] problem about set operation and computation after split

Rui Barradas ruipbarradas at sapo.pt
Wed Jun 13 13:22:54 CEST 2012


Hello,

It seems you haven't discovered the *apply functions yet. In this case, 
since you have a list, the function would be lapply.

1.
Reduce(intersect, lapply(mySplit, function(x) x$product))

2.
lapply(mySplit, function(x) within(x, relative <- diff(c(value[1], value))))

3.
lapply(mySplit, function(x) x[order(x$value), ])


And R-intro.pdf in the doc folder of your installation of R.

Hope this helps,

Rui Barradas

Em 06-06-2012 08:43, bestbird escreveu:
> hi,
>      I met some problems in R, plz help me.
> 1. How to do a intersect operation among several groups in one list, without
> a loop statement? (I think It may be a list)
>     create data:
>     myData<- data.frame(product = c(1,2,3,1,2,3,1,2,2),
> year=c(2009,2009,2009,2010,2010,2010,2011,2011,2011),value=c(1104,608,606,1504,508,1312,900,1100,800))
>     mySplit<- split(myData,myData$year)
>     mySplit
> $`2009`
>    product year value
> 1       1 2009  1104
> 2       2 2009   608
> 3       3 2009   606
>
> $`2010`
>    product year value
> 4       1 2010  1504
> 5       2 2010   508
> 6       3 2010  1312
>
> $`2011`
>    product year value
> 7       1 2011   900
> 8       2 2011  1100
> 9       2 2011   800
>      I want to get intersection of product between every year. I know the
> basic is:
>      intersect(intersect(mySplit[[1]]$product,
> mySplit[[2]]$product),mySplit[[3]]$product)
>      this will give the correct answer:
>      [1] 1 2
>      above code lacks reusability, so It should use a for loop:
>      myIntersect<-mySplit[[1]]$product
>      for (i in 1:length(mySplit)-1){
>          myIntersect<-intersect(myIntersect,mySplit[[i+1]]$product)
>      }
>      It's correct too, but stll too complex, so my question is:
>      Can I do the same thing just use another similar intersect function
> (without for/repeat/while).
>      What's this simple function's name ?
>
> 2.how to do a relative computation after split (notice: not befor split)?
>     create data:
>     myData1<- data.frame(product = c(1,2,3,1,2,3),
> year=c(2009,2009,2009,2010,2010,2010),value=c(1104,608,606,1504,508,1312),relative=0)
>     mySplit1<- split(myData1,myData1$year)
>     mySplit1
> $`2009`
>    product year value relative
> 1       1 2009  1104        0
> 2       2 2009   608        0
> 3       3 2009   606        0
>
> $`2010`
>    product year value relative
> 4       1 2010  1504        0
> 5       2 2010   508        0
> 6       3 2010  1312        0
>     I want compute relative value in the every group, what I mean is , I want
> get the result is just like below:
>     $`2009`
>    product year value relative
> 1       1 2009  1104        0
> 2       2 2009   608        -496
> 3       3 2009   606        -2
>
> $`2010`
>    product year value relative
> 4       1 2010  1504        0
> 5       2 2010   508        -996
> 6       3 2010  1312        804
> I think to use a loop maybe work, but Is there no direct method on list?
>
> 3.how to do a sorting after split, It's just like above question, what I
> want is sorting by value:
>     $`2009`
>    product year value relative
> 3       3 2009   606        0
> 2       2 2009   608        0
> 1       1 2009  1104        0
> $`2010`
>    product year value relative
> 5       2 2010   508        0
> 6       3 2010  1312        0
> 4       1 2010  1504        0
>
> 4. how to do a filtering after split, Yes, It's just like above quetion,
> what I want is filtering out data which value is more than 1000:
> $`2009`
>    product year value relative
> 1       1 2009  1104        0
> $`2010`
>    product year value relative
> 4       1 2010  1504        0
> 6       3 2010  1312        0
>
> --
> View this message in context: http://r.789695.n4.nabble.com/problem-about-set-operation-and-computation-after-split-tp4632496.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.



More information about the R-help mailing list