If you are strict about your data formatting then the following is a fast way
of calculating the differences, based on reshaping the data column:
A = matrix(mydata$rslt, nrow=2)
data.frame(exp=1:ncol(A), diff=A[2,]-A[1,])
alternatively, if the 'exp' values are not guaranteed to be sequential you
Thanks very much. I suspect 50% of my time in R is spent translating
from what I know how to do in SAS (25+ years of heavy use), to what is
equivalent in SAS. So far, I haven't found anything I can do in SAS that
I can't do in R, with some help. ;-)
Cheers...
On 3/17/2017 1:51 PM, Bert Gunter
On 3/17/2017 1:19 PM, Bert Gunter wrote:
Evan:
You misunderstand the concept of a lagged variable.
Well, lag in R, perhaps (and by my own admission). In SAS, thats exactly
how it works.:
data test;
input exp rslt;
cards;
*;
data test2; set test; by exp;
diff=rslt-lag(rslt);
On 3/17/2017 12:58 PM, Ulrik Stervbo wrote:
> Hi Evan
>
> you can easily do this by applying diff() to each exp group.
>
> Either using dplyr:
> library(dplyr)
> mydata %>%
> group_by(exp) %>%
> summarise(difference = diff(rslt))
>
> Or with base R
> aggregate(mydata, by = list(group = mydata
Evan:
Yes, I stand partially corrected. You have the concept correct, but R
implements it differently than SAS.
I think what you want for your approach is diff():
evens <- (seq_len(nrow(mydata)) %% 2) == 0
newdat <-data.frame(exp=mydata[evens,1 ],reslt= diff(mydata[,2])[evens[-1]])
... which s
Evan:
You misunderstand the concept of a lagged variable.
Ulrik:
Well, yes, that is certainly a general solution that works. However,
given the *specific* structure described by the OP, an even more
direct (maybe more efficient?) way to do it just uses (logical)
subscripting:
odds <- (seq_len(
Hi Evan
you can easily do this by applying diff() to each exp group.
Either using dplyr:
library(dplyr)
mydata %>%
group_by(exp) %>%
summarise(difference = diff(rslt))
Or with base R
aggregate(mydata, by = list(group = mydata$exp), FUN = diff)
HTH
Ulrik
On Fri, 17 Mar 2017 at 17:34 Evan C
Suppose I have a dataframe that looks like the following:
n=2
mydata <- data.frame(exp = rep(1:5,each=n), rslt =
c(12,15,7,8,24,28,33,15,22,11))
mydata
exp rslt
11 12
21 15
327
428
53 24
63 28
74 33
84 15
95 22
10 5 11
The variabl
8 matches
Mail list logo