Hi My R version is the current version as at 15 Nov 2013. I have tried to calculate range using tapply() with FUN=range. tapply() returns two fields, the ID field and a field of two text items one is the maximum and the other is the minimum. I take as the difference max - min, does R use a different term for range in tapply? I have also tried aggregate() with Fun=range, with Fun=min and FUN=max and they also gave problems. What is the best route to calculate ranges for groups within a data frame. Thanks. Scriptham. -- View this message in context: http://r.789695.n4.nabble.com/Calculate-Range-tp4680579.html Sent from the R help mailing list archive at Nabble.com.
On 11/17/2013 08:49 AM, SCRIPTHAM wrote:> Hi > > My R version is the current version as at 15 Nov 2013. > > I have tried to calculate range using tapply() with FUN=range. > tapply() returns two fields, the ID field and a field of two text items one > is the maximum and the other is the minimum. > I take as the difference max - min, does R use a different term for range in > tapply? > > I have also tried > aggregate() with Fun=range, with Fun=min and FUN=max > and they also gave problems. > > What is the best route to calculate ranges for groups within a data frame. >Hi Scriptham, It looks like you want to get the difference between the maximum and minimum values rather than the actual values. Define a function: range_span<-function(x) return(diff(range(x))) and use that as the FUN argument. Jim
An approach using data tables:
###
library(data.table)
# dt: some data arranged by group
dt <- data.table(group=c(rep("a",5), rep("b",10),
rep("c",15)), values=1:30)
# summarize by group
smry <- dt[,list(min=min(values), max=max(values),
range=diff(range(values))), by="group"]
smry
###
-----Original Message-----
From: SCRIPTHAM [mailto:JML at CWAZY.CO.UK]
Sent: Saturday, November 16, 2013 4:50 PM
To: r-help at r-project.org
Subject: [R] Calculate Range
Hi
My R version is the current version as at 15 Nov 2013.
I have tried to calculate range using tapply() with FUN=range.
tapply() returns two fields, the ID field and a field of two text items one is
the maximum and the other is the minimum.
I take as the difference max - min, does R use a different term for range in
tapply?
I have also tried
aggregate() with Fun=range, with Fun=min and FUN=max and they also gave
problems.
What is the best route to calculate ranges for groups within a data frame.
Thanks.
Scriptham.
--
View this message in context:
http://r.789695.n4.nabble.com/Calculate-Range-tp4680579.html
Sent from the R help mailing list archive at Nabble.com.
Hi,
You may also check:
library(psych)
library(plyr)
df1 <- data.frame(group=rep(letters[1:3],c(5,10,15)), values=1:30)
ddply(df1,.(group),mutate,Range=describe(values)$range) ##depends on how you
wanted the output
#or
ddply(df1,.(group),summarise,Range=describe(values)$range)
#or
with(df1,describeBy(values,group,mat=TRUE))[,c("group1","range")]
A.K.
On Sunday, November 17, 2013 12:02 AM, SCRIPTHAM <JML at CWAZY.CO.UK>
wrote:
Hi
My R version is the current version as at 15 Nov 2013.
I have tried to calculate range using tapply() with FUN=range.
tapply() returns two fields, the ID field and a field of two text items one
is the maximum and the other is the minimum.
I take as the difference max - min, does R use a different term for range in
tapply?
I have also tried
aggregate() with Fun=range, with Fun=min and FUN=max
and they also gave problems.
What is the best route to calculate ranges for groups within a data frame.
Thanks.
Scriptham.
--
View this message in context:
http://r.789695.n4.nabble.com/Calculate-Range-tp4680579.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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.