Arnold Akem
2009-Jun-26 03:31 UTC
[R] code that will use two data sets which differ in size.
Hi all, This is a really basic question but I can't figure it out. I am trying to write a piece of code that will use two datasets, z and m.. This code is meant to assign the mean of selected values in dataset m to a new column z$c in datset z but the rows and columns of the two data sets differ. I managed to write this down but think I am missing something standard but basic,I will really appreciate if someone could point out where I' am going wrong or help me out with a piece of code for this.Google did not help me even and the R manuals gave me hints. "The shoulders will never grow beyond the head". for (k in 1:nrow(z)) + for (g in 1:nrow(m)) + for (l in 1:3) + for (j in 15:43){ + z$ccs[k]<-mean(m[m$soi[g]==l&m$week[g]==j,]$CCS,na.rm=TRUE)} [[alternative HTML version deleted]]
Petr PIKAL
2009-Jun-26 10:21 UTC
[R] Odp: code that will use two data sets which differ in size.
Petr Pikal petr.pikal at precheza.cz 724008364, 581252140, 581252257 r-help-bounces at r-project.org napsal dne 26.06.2009 05:31:27:> Hi all, > > This is a really basic question but I can't figure it out. > > I am trying to write a piece of code that will use two datasets, z andm..> This code is meant to assign the mean of selected values in dataset m toa new> column z$c in datset z but the rows and columns of the two data setsdiffer.> > I managed to write this down but think I am missing something standardbut> basic,I will really appreciate if someone could point out where I' amgoing> wrong or help me out with a piece of code for this.Google did not helpme even> and the R manuals gave me hints. > > "The shoulders will never grow beyond the head". > > for (k in 1:nrow(z)) > + for (g in 1:nrow(m)) > + for (l in 1:3) > + for (j in 15:43){ > + z$ccs[k]<-mean(m[m$soi[g]==l&m$week[g]==j,]$CCS,na.rm=TRUE)}Maybe that I have no idea what is z I get an error too.> for (k in 1:nrow(z))+ + for (g in 1:nrow(m)) + + for (l in 1:3) + + for (j in 15:43){ + + z$ccs[k]<-mean(m[m$soi[g]==l&m$week[g]==j,]$CCS,na.rm=TRUE)} Error in nrow(z) : object 'z' not found What kind of error do you have? If you do not post some reproducible example it would be quite difficult to understand what you want to get as a result. I believe some *apply trick could help but without knowing what you want it could be just rough guessing. Regards Petr> > > > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi , Thanks for the timely reply in,z is the larger dataframe and m is the shorter dataframe. The code took more than 2 hrs and I had to kill it. Another way putting this question could be; how can I use th family of (apply) funtions to get the summary of one of the variables based on two factors? Thanks again, arnoldo wrote:> > Hi all, > > This is a really basic question but I can't figure it out. > > I am trying to write a piece of code that will? use two datasets, z and > m.. > This code is meant to assign the mean of selected values in dataset m to a > new column z$c in datset z but the rows and columns of the two data sets > differ. > > I managed to write this down but think I am missing something standard but > basic,I will really appreciate if someone could point out where I' am > going wrong or help me out with a piece of code for this.Google did not > help me even and the R manuals gave me hints. > > "The shoulders will never grow beyond the head". > > for (k in 1:nrow(z)) > + for (g in 1:nrow(m)) > + for (l in 1:3) > + for (j in 15:43){ > + z$ccs[k]<-mean(m[m$soi[g]==l&m$week[g]==j,]$CCS,na.rm=TRUE)} > > > > > > > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/code-that-will-use-two-data-sets-which-differ-in-size.-tp24214485p24239706.html Sent from the R help mailing list archive at Nabble.com.
John Kane
2009-Jun-28 16:44 UTC
[R] code that will use two data sets which differ in size.
If I understand what you want, you simply want to calculate some means in one data.frame and put some of them in another data.frame? Data.frames are of unequal lenght but you know the indices for each. As long as you know the indices the relative sizes should be irrelevant. Try something like this. It's a bit ugly but I think it will work mydata <- data.frame(a = 1:10, b=11:20, c=21:30) # frame to get means adat <- data.frame(dd=rnorm(20,5,2)) # target data.frame mm <- (mydata[,2]+mydata[,3])/2; mm #Calculate means adat[c(1,3),2] <- mm[c(2,7)] # Get means from vector into target --- On Sun, 6/28/09, arnoldo <a_akem2001 at yahoo.co.uk> wrote:> From: arnoldo <a_akem2001 at yahoo.co.uk> > Subject: Re: [R] code that will use two data sets which differ in size. > To: r-help at r-project.org > Received: Sunday, June 28, 2009, 4:40 AM > > Hi , > > Thanks for the timely reply in,z is the larger dataframe > and m is the > shorter dataframe. > The code took more than 2 hrs and I had to kill it. > > Another way putting this question could be; how can I use > th family of > (apply) funtions to get the summary of one of the variables > based on two > factors? > > Thanks again, > > > arnoldo wrote: > > > > Hi all, > > > > This is a really basic question but I can't figure it > out. > > > > I am trying to write a piece of code that will? use > two datasets, z and > > m.. > > This code is meant to assign the mean of selected > values in dataset m to a > > new column z$c in datset z but the rows and columns of > the two data sets > > differ. > > > > I managed to write this down but think I am missing > something standard but > > basic,I will really appreciate if someone could point > out where I' am > > going wrong or help me out with a piece of code for > this.Google did not > > help me even and the R manuals gave me hints. > > > > "The shoulders will never grow beyond the head". > > > > for (k in 1:nrow(z)) > > + for (g in 1:nrow(m)) > > + for (l in 1:3) > > + for (j in 15:43){ > > + > z$ccs[k]<-mean(m[m$soi[g]==l&m$week[g]==j,]$CCS,na.rm=TRUE)} > > > > > > > > > > > > > > > > > > > >? ? ??? > > ??? [[alternative HTML version > deleted]] > > > > > > ______________________________________________ > > 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. > > > > > > -- > View this message in context: http://www.nabble.com/code-that-will-use-two-data-sets-which-differ-in-size.-tp24214485p24239706.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. >__________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/