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
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
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
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
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
)
.
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
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.
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)))
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
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
-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
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),
12 matches
Mail list logo