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 gunter.ber...@gene.com An: Johannes Radinger johannesradin...@gmail.com Cc: R help r-help@r-project.org Gesendet: Mittwoch, 26. März 2014 16:45:43 GMT+00:00 Betreff: Re: [R] dataframe

[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 the base value (where var1=c) which is

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

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 johannesradin...@gmail.com 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 calculate relative

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 == c], result_calc = var3/div )

Re: [R] Dataframe calculations

2010-03-22 Thread Petr PIKAL
. Thank you, Mike -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erich Neuwirth Sent: Friday, March 19, 2010 1:33 PM To: r-help@r-project.org Subject: Re: [R] Dataframe calculations Sorry, Oddly I got the use of odds

[R] Dataframe calculations

2010-03-19 Thread Hosack, Michael
Hi everyone, My question will probably seem simple to most of you, but I have spent many hours trying to solve it. I need to perform a series of sequential calculations on my dataframe that move across rows and down columns, and then repeat themselves at each unique 'MM' by 'DD' grouping.

Re: [R] Dataframe calculations

2010-03-19 Thread Erich Neuwirth
with the following code newvars()$ARRIVALS and newvars()$DEPARTURES will give you the new variables you need. -=-=-= addDelays - function(arriveTime,waitVec,travelVec){ start-as.POSIXct(arriveTime,format=%H:%M:%S) delays-as.vector(t(cbind(waitVec,travelVec)))

Re: [R] Dataframe calculations

2010-03-19 Thread Hosack, Michael
Of Erich Neuwirth Sent: Friday, March 19, 2010 11:38 AM To: r-help@r-project.org Subject: Re: [R] Dataframe calculations with the following code newvars()$ARRIVALS and newvars()$DEPARTURES will give you the new variables you need. -=-=-= addDelays - function(arriveTime,waitVec,travelVec){ start

Re: [R] Dataframe calculations

2010-03-19 Thread Erich Neuwirth
and see why it did not yield the correct results. Thank you, Mike -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erich Neuwirth Sent: Friday, March 19, 2010 11:38 AM To: r-help@r-project.org Subject: Re: [R] Dataframe

Re: [R] Dataframe calculations

2010-03-19 Thread Hosack, Michael
-project.org] On Behalf Of Erich Neuwirth Sent: Friday, March 19, 2010 1:33 PM To: r-help@r-project.org Subject: Re: [R] Dataframe calculations Sorry, Oddly I got the use of odds and evens the wrong way round. addDelays - function(arriveTime,waitVec,travelVec){ start-as.POSIXct(arriveTime,format=%H:%M:%S

Re: [R] Dataframe calculations

2010-03-19 Thread jim holtman
try this: # add 'date' to separate the data SCHEDULE2 - within(SCHEDULE2, { date - paste(YEAR, '-', MM, '-', DD, sep='') ARRIVE - as.POSIXct(paste(date, ARRIVE)) DEPART - as.POSIXct(paste(date, DEPART)) }) # process each day result - lapply(split(SCHEDULE2, SCHEDULE2$date),