[R] Comparing rows in a dataframe
andy_liaw at merck.com
Fri Aug 6 13:53:18 CEST 2004
I would sort the data frame by the grouping variables and order of the
scores, then use tapply and diff to compute the successive differences by
groups. You may want to pad the output by a zero to make it the same length
as the original scores.
> From: Doran, Harold
> I have a longitudinal dataframe organized in the long format
> and would like to make comparison between successive rows if
> certain conditions apply. Specifically, I have four variables
> of interest: grade, score, year, and schid, associated with
> each school with 3 measurements per school per grade,
> therefore the rows are temporally ordered and each school
> occupies multiple rows. For example, a school may have grades
> 4,5,6 and three observations per grade at years 2001-2003 and
> therefore occupies 9 rows.
> So, my goal is to make a comparison between score 1 and 2
> (same column, next row) if the score is from the same school,
> same grade, but one year later and create a new binary
> variable if score 1 < score 2.
> Something like, ifelse(data[x,4] with data[x,4], if
> schid=schid and grade = grade, and year = year + 1, 1, 0)
> So, I will end up with a new variable in the data frame where
> 1 indicates that subsequent score is larger than the score
> for the same grade in the same school in year t-1.
> I'm a little confused how conditional selection would work in
> such a scenario.
> Many thanks,
> [[alternative HTML version deleted]]
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide!
More information about the R-help