align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

Chicago R User Group (Chicago RUG) Data and Statistics Message Board › problem about set operation and computation after split

problem about set operation and computation after split

James B.
user 50325812
Chicago, IL
Post #: 1
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,201­1,2011,2011),value=c(1104,608,606,1504,5­08,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]]$produ­ct)
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,my­Split[[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),va­lue=c(1104,608,606,1504,508,1312),relati­ve=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

James B.
user 50325812
Chicago, IL
Post #: 3
can anyone help me?
Paul T.
user 11836216
Group Organizer
Chicago, IL
Post #: 3
James,

If you want help with R, I strongly suggest using the R Help mailing list. Visit http://www.r-project.org/mail.html­ for details of using the list. Be certain you read the posting guide before posting any question to the list.

Very few people read the home page for the Chicago R Users Group, so you are unlikely to get answers for questions posted there.

Paul
Powered by mvnForum

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy