Krishnan Viswanathan
2012-Apr-03 00:00 UTC
[R] Compare by row and insert previous row value (Or non Time Series Lag)
I have the following sample dataset (CSV input here:http://goo.gl/YR8LP. CSV output here: http://goo.gl/EFCC8) which I want to transform as follows. For each person in a household I want to create two new variables OrigTAZ and DestTAZ. It should take the value in TripendTAZ and put that in DestTAZ. For OrigTAZ it should put value of TripendTAZ from the previous row. For the first trip of every person in a household (Tripid = 1) the OrigTAZ = hometaz. For each person in a household, from the second trip OrigTAZ = TripendTAZ_(n-1) (the value of TripendTAZ from the previous row) and DestTAZ = TripEndTAZ. The sample input and output data are shown in the links above. I tried the suggestions shown here: Basic lag in R vector/dataframe<http://stackoverflow.com/questions/3558988/basic-lag-in-r-vector-dataframe> but have not had luck. TIA, Krishnan -- Krishnan Viswanathan 1101 High Meadow Dr Tallahassee FL 32311 [[alternative HTML version deleted]]
Ashish Agarwal
2012-Apr-04 05:30 UTC
[R] Compare by row and insert previous row value (Or non Time Series Lag)
Hello my old friend. Remember me from Cambridge. Here is the dirty way of getting your desired output and enjoy hassling with trips that are incomplete or missing information inpfil <- read.csv("sample_trip_od_input.csv") aa <- split(inpfil, paste(inpfil[,1],inpfil[,2],sep=',')) OrigTAZ <- inpfil[,5] DestTAZ <- OrigTAZ cx <- cbind(OrigTAZ, DestTAZ) out.data.fr <- cbind(inpfil[FALSE,], cx[FALSE,]) for( i in 1:length(aa)) { bb <- as.data.frame(aa[i]) for( j in 1:5) {names(bb)[j] <- names(inpfil)[j]} bb.len <- length(bb) OrigTAZ <- bb[1:(bb.len-1),5] DestTAZ <- bb[2:bb.len,5] cc <- cbind(OrigTAZ, DestTAZ) cc.fill <- cbind(bb[1,4],cc[1,1]) cc <- rbind(cc.fill, cc) bb <- cbind(bb,cc) out.data.fr <- rbind(out.data.fr,bb) } out.data.fr AA +1 (617) 595-1068 On Tue, Apr 3, 2012 at 5:30 AM, Krishnan Viswanathan < krisviswanathan@gmail.com> wrote:> I have the following sample dataset (CSV input here:http://goo.gl/YR8LP. > CSV output here: http://goo.gl/EFCC8) which I want to transform as > follows. > For each person in a household I want to create two new variables OrigTAZ > and DestTAZ. It should take the value in TripendTAZ and put that in > DestTAZ. For OrigTAZ it should put value of TripendTAZ from the previous > row. For the first trip of every person in a household (Tripid = 1) the > OrigTAZ = hometaz. For each person in a household, from the second trip > OrigTAZ = TripendTAZ_(n-1) (the value of TripendTAZ from the previous row) > and DestTAZ = TripEndTAZ. The sample input and output data are shown in the > links above. I tried the suggestions shown here: Basic lag in R > vector/dataframe< > http://stackoverflow.com/questions/3558988/basic-lag-in-r-vector-dataframe > > > but > have not had luck. > > TIA, > Krishnan > -- > Krishnan Viswanathan > 1101 High Meadow Dr > Tallahassee FL 32311 > >[[alternative HTML version deleted]]