Re: [R] dataframe calculations based on certain values of a column

2014-03-27 Thread johannesradin...@gmail.com
Thanks, your solution using ave() works perfectly. /johannes -Ursprüngliche Nachricht- Von: Bert Gunter An: Johannes Radinger Cc: R help Gesendet: Mittwoch, 26. März 2014 16:45:43 GMT+00:00 Betreff: Re: [R] dataframe calculations based on certain values of a column I believe this

Re: [R] dataframe calculations based on certain values of a column

2014-03-26 Thread Noah Marconi
dplyr's group_by and mutate can create those columns for you: var1 <- c("a","b","c","a","b","c","a","b","c") var2 <- c("X","X","X","Y","Y","Y","Z","Z","Z") var3 <- c(1,2,2,5,2,6,7,4,4) df <- data.frame(var1,var2,var3) dt <- tbl_df(df) dt %.% group_by(var2) %.% mutate( div = var3[var1 =

Re: [R] dataframe calculations based on certain values of a column

2014-03-26 Thread Berend Hasselman
On 26-03-2014, at 17:09, Johannes Radinger wrote: > Hi, > > I have data in a dataframe in following structure > var1 <- c("a","b","c","a","b","c","a","b","c") > var2 <- c("X","X","X","Y","Y","Y","Z","Z","Z") > var3 <- c(1,2,2,5,2,6,7,4,4) > df <- data.frame(var1,var2,var3) > > Now I'd like to

Re: [R] dataframe calculations based on certain values of a column

2014-03-26 Thread Bert Gunter
I believe this will generalize. But check carefully! Using your example (Excellent!), use ave(): with(df,ave(seq_along(var1),var2,FUN=function(i) var3[i]/var3[i][var1[i]=="c"])) [1] 0.500 1.000 1.000 0.833 0.333 1.000 1.750 [8] 1.000 1.000 This is kind of a l

[R] dataframe calculations based on certain values of a column

2014-03-26 Thread Johannes Radinger
Hi, I have data in a dataframe in following structure var1 <- c("a","b","c","a","b","c","a","b","c") var2 <- c("X","X","X","Y","Y","Y","Z","Z","Z") var3 <- c(1,2,2,5,2,6,7,4,4) df <- data.frame(var1,var2,var3) Now I'd like to calculate relative values of var3. This values should be relative to th