Finding the column index where a row changes values in R dataframe/Datatable -
suppose have following dataframe:
df <- data.frame(col1=c(2,2,1),col2=c(2,7,5),col3=c(9,6,4))
i know 2 things:
how can field stating index of column row changes values. hence, first row we'd 1 , 3, , second , third rows, 1, 2 , 3.
how field of "before , after"? first case, 2 -> 9, second , third, 2->7, 7-> 6 , 1 ->5, 5->4, respectively.
what this:
x <- sapply(1:ncol(df), function(x) rle(df[x,])$values)
output of x:
[[1]] col2 col3 1 2 9 [[2]] col1 col2 col3 2 2 7 6 [[3]] col1 col2 col3 3 1 5 4
then if you'd full range of before/after values, use:
lapply(x,function(i) paste0(i,collapse="->")) [[1]] [1] "2->9" [[2]] [1] "2->7->6" [[3]] [1] "1->5->4"
Comments
Post a Comment