Hi Andrew,
Just remember that something was bad with the code I sent you (the same you
referred to in [1]).
This version runs with no problems:
# Some data
history_ <-
data.frame(person_id=c(1,2,2),date_=c("2009-01-01","2009-02-03","2009-02-02"),x=c(0.01,0.05,0.06))
colnames(history_) <- c("perso
On Fri, Feb 27, 2009 at 2:10 PM, William Dunlap wrote:
> Andrew, it makes it easier to help if you supply a typical
> input and expected output along with your code. I tried
> your code with the following input:
I'll be careful to avoid these mistakes. Also, I should not have used
a reserved wo
[,"person_id"]),,drop=FALSE]
history[order(history$date),,drop=FALSE]
}
> f2(history)
person_id date
2 Mary2
7 Alex7
9Sue9
10 Joe 10
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
----
Dear Andrew:
Here is another way assuming you have an order column in your history data
as well as a person_id. Again, your variable
of interest is x:
# Some data
set.seed(1)
history<-data.frame(
person_id=rep(1:10,each=10),
record=rep(sample(10),10),
x=rnorm(100)
"...from an SQL database."How? Structure of the result?
You say "ordered by date" but then you don't reference any date
variable? And your code creates an "order" column, but that would not
appear necessary for the stated purpose and you don't output the last
"order" within a "person_i
Dear Andrew,
Here is one way:
# Some data
set.seed(1)
mydata<-data.frame(person_id=rep(1:10,10),x=rnorm(100))
mydata
# last register for person_id
with(mydata,tapply(x,person_id,function(x) tail(x,1)))
# test for person_id=1
mydata[mydata$person_id==1,] # see the last number in column 2
HTH,
I want to find the last record for each person_id in a data frame
(from a SQL database) ordered by date. Is there a better way than
this for loop?
for (i in 2:length(history[,1])) {
if (history[i, "person_id"] == history[i - 1, "person_id"])
history[i, "order"] = history[i - 1, "order"]
7 matches
Mail list logo