parishodak
parishodak

Reputation: 4666

How to update row values based on other row values in R?

I have a data frame which has following values:

Id CreationDate Value ChangeCounter
1   date1         p01     1
2   date2         p01     1
3   date3         p02     2
4   date4         p02     2
5   date5         p01     3
6   date6         p03     4

Here "ChangeCounter" is a new column that needs to be added to the data frame. The values in the Change counter, which basically tracks how many times the Value column changed.

Ultimately, I am trying to get the list of first rows whenever the Value field is changed. Hence the output will be:

Output:

Id CreationDate Value ChangeCounter
1   date1         p01     1
3   date3         p02     2
5   date5         p01     3
6   date6         p03     4

Most of the examples I read operates on columns, do the transformation and add a new column value for the row. But the example for my requirement is eluding me. If there is any other better way to achieve the end result without computed "ChangeCounter" column will also give a different view point solving the issue.

Upvotes: 0

Views: 791

Answers (2)

echasnovski
echasnovski

Reputation: 1181

If you want to have rows at which change of Value occurred then this might help:

df[c(TRUE, head(df$Value, n = -1) != tail(df$Value, n = -1)), ]

If you want the exact ChangeCounter column then:

df$ChangeCounter <- cumsum(c(TRUE, head(df$Value, n = -1) !=
                                     tail(df$Value, n = -1)))

Upvotes: 1

G5W
G5W

Reputation: 37641

Without the change counter:

GoodRows = c(1,which(df$Value[-1] != df$Value[-nrow(df)]) +1)
df[GoodRows,]
  Id CreationDate Value
1  1        date1   p01
3  3        date3   p02
5  5        date5   p01
6  6        date6   p03

Upvotes: 2

Related Questions