[R] data frame question

Andras Farkas motyocska at yahoo.com
Sun Aug 6 13:10:53 CEST 2017


Dear All,

wonder if you have thoughts on the following: 

let us say we have:

df<-data.frame(a=c(1,2,3,4,5,1,2,3,4,5,6,7,8),b=c(0,1,2,3,4,0,1,2,3,4,5,6,7))


 I would like to rewrite values in column name "a" based on values in column name "b", where based on a certain value of column "b" the next value of column 'a' is prompted, in other words would like to have this as a result:

df<-data.frame(a=c(1,1,1,1,1,2,2,2,2,2,2,2,2),b=c(0,1,2,3,4,0,1,2,3,4,5,6,7)) 


where at the value of 0 in column 'b' the number in column a changes from 1 to 2. From the first zero value of column 'b' and until the next zero in column 'b' the numbers would not change in 'a', ie: they are all 1 in my example... then from 2 it would change to 3 again as 'b' will have zero again in a row, and so on.. Would be grateful for a solution that would allow me to set the values (from 'b') that determine how the values get established in 'a' (ie: lets say instead of 0 I would want 3 being the value where 1 changes to 2 in 'a') and that would be flexible to take into account that the number of rows and the number of time 0 shows up in a row in column 'b' may vary...

much appreciate your thoughts..

Andras



More information about the R-help mailing list