[19164] in s-news-athena
FW: [S] Question on data manipulation
daemon@ATHENA.MIT.EDU (Abd Rahman Kassim)
Thu Sep 9 04:27:07 1999
Message-Id: <01BEFADF.382125A0.rahmank@frim.gov.my>
From: Abd Rahman Kassim <rahmank@frim.gov.my>
To: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
Cc: "'Prof Brian D Ripley'" <ripley@stats.ox.ac.uk>
Date: Thu, 9 Sep 1999 16:20:22 +0800
Dear S-plus user,
Some error in my earlier mail (see below). The third solution was written as given by Bill Venables. It should be by Brian Ripley.
Abd Rahman
Hill Forest Silviculture
FRIM
-----Original Message-----
From: Abd Rahman Kassim [SMTP:rahmank@frim.gov.my]
Sent: Thursday, September 09, 1999 11:52 AM
To: s-news@wubios.wustl.edu
Subject: [S] Question on data manipulation
Dear S-Plus user,
Thanks to the prompt responses by Dr. Bill Dunlap, Dr. Scott Saleska, Prof.
Brian D. Ripley and Dr. S.H. Heisterkamp and Dr. Gerald Jean. The
solution is not so simple as I thought. Need further learning and
understand of the right code to use. The question and solution is shown
below. I have tried the first three solutions and it work well with my data
set. Preferred solution for my actual research data set is by Bill Dunlap.
Thanks again for all your kind assistance.
Abd Rahman
Hill Forest Silviculture
FRIM
QUESTION
My question is on how to write S-plus code to calculate the value of
> "relative position" for a particular subject of that variable. For
> example,
>
> Data file: tree.dat
> Variables : Plot : Size of the plot
> DBH : Diameter of the subject tree
> DBHL: Total diameter of trees for trees larger than
> the
> subject tree
>
> Plot DBH DBHL
> ------ ------------ ---------
> a 10 40
> a 15 0
> a 13 15
> a 12 28
> b 5 30
> b 10 13
> b 7 23
> b 13 0
>
> In this example, my question is how to write S-plus code, to calculate
> the
> value of DBHL.
> I am using S-plus for Windows Version 4.5 Rel. 2.
================
SOLUTION given by the respective adviser.
Bill Dunlap wrote;
f<-function(data)
{
i <- order(data$Plot, - data$DBH)
data$DBHL <- data$DBH
data$DBHL[i] <- unlist(lapply(split(data$DBH[i], data$Plot[i]),
cumsum)
) - data$DBH[i]
data
}
E.g., with a data frame d:
> f(d)
++++++++++++++++++
Dr. Scott Saleska wrote;
attach(tree.dat)
DBHL<-tapply( tree.dat$DBH, tree.dat$Plot, function(x)
{
y <- rep(0, length(x))
for( i in 1:length(x) ) y[i] <- sum( x[x>x[i]] )
y }
)
DBHL
data.frame ( tree.dat, DBHL=c(DBHL$a, DBHL$b) )
+++++++++++++++
Brian Ripley wrote (not Bill Venables) :
lg <- function(x)
{
ind <- rev(order(x))
y <- cumsum(x[ind])
y <- c(0, y[-length(y)])
x[ind] <- y
x
}
> lg(c(10, 15, 13, 12))
[1] 40 0 15 28
This does not handle ties, but is faster for large groups.
+++++++++++++++
Dr. S.H. Heisterkamp wrote;
tree.dat$DBHL<-NULL
which.plots<-unique(tree.dat$plot)
for (pl in wich.plots){
dbh.plot<-tree.dat[tree.dat$plot==pl,2]
for (i in 1:length(dbh.plot)){
tree.dat$DBHL<c(tree.dat$DBHL,sum(dbh.plot[-i]))
}
}
+++++++++++++++++++++
Gerald Jean wrote;
"testsum" <- function (ttt.frame)
{
ttt.split <- lapply(split(ttt.frame$DBH, ttt.frame$Plot), FUN =
function(x)
{my.mat <- as.matrix(x)
apply(my.mat, 1, FUN = function(x,y)
{sum(y[y > x])}, my.mat)})
DBHL <- rep(0,nrow(ttt.frame))
start <- 1
for (i in 1:length(ttt.split))
{end <- start + length(ttt.split[[i]]) - 1
DBHL[start:end] <- ttt.split[[i]]
start <- end + 1
}
DBHL
}
Testing the function:
> ttt.frame
> ttt.frame$DBHL_testsum(ttt.frame)
> ttt.frame
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news