[R] Calculate the difference using ave

Patrick Hausmann c18g at uni-bremen.de
Thu Oct 27 16:57:29 CEST 2011


Thanks Dimitris,

but I would like to bind the result on the dataframe, so the length 
should be equal to nrow(df1).

BTW, sorry for the example, it wasn't very clear, next try:

#####################################################################

options(stringsAsFactors = FALSE)

set.seed(123)
df1 <- data.frame(id = rep(LETTERS[1:6], 3),
                   yr = rep(c(2009:2011), each=6),
                   water = sample(c(100:500), 18),
                   salt  = sample(c(10:40), 18))

CalcDiffPct <- function(xdf) {			
               n <- length(unique(xdf[["id"]]))
               n.NA <- rep(NA, n)
               w <- seq_len(nrow(xdf) - n)
               diff_pct <- xdf$salt / c(n.NA, xdf$water[w]) * 100
               diff_pct
               }

# The order is important
df1 <- df1[order(df1$yr, df1$id), ]

# This works, as long as each
# combination of "yr" / "id" exist
with(df1, table(id, yr))
df1$salt_pct <- CalcDiffPct(df1)
df1

# But if the I drop any row the result will be wrong
# (or 'correct' as the function doesn't handle this case)
df2 <- df1
df2 <- df2[-15, ]
with(df2, table(id, yr))
df2$salt_pct2 <-  CalcDiffPct(df2)
df2

##############################################################

Thanks for any help!
Patrick


Am 26.10.2011 14:00, schrieb Dimitris Rizopoulos:
> Maybe one approach could be:
>
> set.seed(123)
> df1 <- data.frame(measure = rep(c("A1", "A2", "A3"), each=3),
> water = sample(c(100:200), 9),
> tide = sample(c(-10:+10), 9))
>
>
> 100 * tail(df1$tide, -3) / head(df1$water, -3)
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
>
> On 10/26/2011 12:02 PM, Patrick Hausmann wrote:
>> Dear R users,
>>
>> It may be very simple but it is being difficult for me.
>> I'd like to calculate the difference in percent between to measures.
>> My data looks like this:
>>
>> set.seed(123)
>> df1 <- data.frame(measure = rep(c("A1", "A2", "A3"), each=3),
>> water = sample(c(100:200), 9),
>> tide = sample(c(-10:+10), 9))
>> df1
>>
>> # What I want to calculate is:
>> # tide_[A2] / water_[A1],
>> # tide_[A3] / water_[A2]
>>
>> # This 'works' for the example, but I am
>> # looking for a more general solution.
>>
>> df1$tide_diff <- ave(df1$tide, FUN=function(L) L /
>> c(NA, NA, NA, df1$water)) * 100
>> df1
>>
>> Thanks for any help!
>> Patrick
>>
>> ______________________________________________
>> 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