Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find index of change in a column

Tags:

r

Is there a way to find the indices of change of factors in a column with R? For example:

x <- c("aaa", "aaa", "aaa", "bbb", "bbb", "ccc", "ddd")

would return 3, 5, 6

like image 890
Jonathan C Lee Avatar asked Sep 09 '15 21:09

Jonathan C Lee


People also ask

What is the index of a column?

An index is a database structure that provides quick lookup of data in a column or columns of a table.

How do you find the index of a column of a DataFrame in Python?

You can get the column index from the column name in Pandas using DataFrame. columns. get_loc() method. DataFrame.

How do I change the index of a column?

If you want to keep the original index as a column, use reset_index() to reassign the index to a sequential number starting from 0 . You can change the index to a different column by using set_index() after reset_index() .


Video Answer


3 Answers

You could try to compare shifted vectors, e.g.

which(x[-1] != x[-length(x)])
## [1] 3 5 6

This will work both on characters and factors

like image 126
David Arenburg Avatar answered Sep 26 '22 17:09

David Arenburg


which(!!diff(as.numeric(x)))
[1] 3 5 6

The assumption is that you really have factors. They are saved internally with numerical values. So when the difference is taken, a one will result at every change. A second coercion is that zeroes are considered FALSE and other numbers TRUE. which locates the TRUE values aka non-zeroes.

like image 21
Pierre L Avatar answered Sep 26 '22 17:09

Pierre L


rle can be used for this:

head(cumsum(rle(x)$lengths), -1)
[1] 3 5 6
like image 22
Matthew Lundberg Avatar answered Sep 22 '22 17:09

Matthew Lundberg