[R] Data frame divison by another data frame with common groups and different length

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 18 20:34:30 CEST 2012


Hello,

Try the following.

agg <- aggregate(buddleiat ~ samplet + datet, data = traffic, FUN = mean)
mrg <- merge(encounters, agg,
         by.x = c("samplec", "datec"),
         by.y = c("samplet", "datet"))

mrg$Div <- with(mrg, Bladen/buddleiat)

Hope this helps,

Rui Barradas
Em 18-09-2012 12:17, Marta Miguel escreveu:
> Dear all,
>
>
> I have two different data frames, that have two common variables: date and
> sample. Here is a small extract of both of them
>
>> head(traffic)
>            datet    sessiont samplet buddleiat
> 1   07-08-2012        1       1         1
> 2   07-08-2012        1       1         1
> 3   07-08-2012        1       1         1
> 4   07-08-2012        1       2         3
> 5   07-08-2012        1       2         1
> 6   07-08-2012        1       2         2
> 7   07-08-2012        2       3         4
> 8   07-08-2012        2       3         5
> 9   07-08-2012        2       3         5
> 10  07-08-2012        2       4         8
> 11  07-08-2012        2       4         4
> 12  07-08-2012        2       4         6
> 13  08-08-2012        1       1         9
> 14  08-08-2012        1       1        12
> 15  08-08-2012        1       1         7
> (...)
>
>> head(encounters)
>            datec     samplec  Bladen
> 1    07-08-2012      1          9
> 2    07-08-2012      1          6
> 3    07-08-2012      1          8
> 4    07-08-2012      1          8
> 5    07-08-2012      1          5
> 6    07-08-2012      1          4
> 7    07-08-2012      1          7
> 8    07-08-2012      1          6
> 9    07-08-2012      1          4
> 10   07-08-2012     1          2
> 11   07-08-2012     1          7
> 12   07-08-2012     1          8
> (...)
>
> They don't have the same length.
>
> Using the function tapply, I managed to calculate the mean of the column
> buddleiat (from traffic) by sample and date.
>
> tapply(buddleiat, list(samplet,datet), mean)
>
>
> Now I want to divide each different value of Bladen by the value that has
> the same number of sample and date of the buddleiat mean calculated. Being
> that, different values from Bladen can be divided by the same value of
> buddleiat mean.
>
>
> I tried using this code:
>
>> budt=(Bladen/tapply(buddleiat, list(samplet,datet), mean))
> Error: dims [product 68] do not match the length of object [1360]
>
> But they don't have the same length, and I am not sure that it will do what
> I am asking.
>
>
> Do you have any suggestions? Is there a function I could use or will it be
> easier to do it "manually" by creating a third data frame with both
> variables and repeated values of the buddleiat means?
>
>
> I hope I made myself understand.
>
>
> Thanks in advance,
>
> Marta Miguel
>
> 	[[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.




More information about the R-help mailing list