Arrebimbomalho
Arrebimbomalho

Reputation: 186

Finding the column index where a row changes values in R dataframe/Datatable

Suppose I have the following dataframe:

DF <- data.frame(Col1=c(2,2,1),Col2=c(2,7,5),Col3=c(9,6,4))

I would like to know 2 things:

Upvotes: 1

Views: 338

Answers (2)

www
www

Reputation: 4224

What about 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 like the full range of before/after values, you could use:

lapply(x,function(i) paste0(i,collapse="->"))

[[1]]
[1] "2->9"

[[2]]
[1] "2->7->6"

[[3]]
[1] "1->5->4"

Upvotes: 1

d.b
d.b

Reputation: 32548

apply(X = DF, MARGIN = 1, function(x) cumsum(rle(x)$lengths))
#[[1]]
#Col1 Col3 
#   2    3 

#[[2]]
#Col1 Col2 Col3 
#   1    2    3 

#[[3]]
#Col1 Col2 Col3 
#   1    2    3 

apply(X = DF, MARGIN = 1, function(x){
    temp = unique(x)
        if (length(temp) == 1){
            temp
        }else{
            sapply(1:(length(temp)-1), function(i)
                paste(temp[i:min(i+1, length(temp))], collapse = ">"))
        }
    })
#[[1]]
#[1] "2>9"

#[[2]]
#[1] "2>7" "7>6"

#[[3]]
#[1] "1>5" "5>4"

Upvotes: 1

Related Questions