[R] code that will use two data sets which differ in size.

John Kane jrkrideau at yahoo.ca
Sun Jun 28 18:44:15 CEST 2009


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/




More information about the R-help mailing list