R - How can I check if a value in a row is different from the value in the previous row?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 15:48:28

We create a logical index ('ind') by comparing the current row with the next row (we can do that by removing the 1st and last element of the 'farm' column for comparison), and also include the condition that the element is not "NULL". Based on the logical index, we can change the TRUE to 'New' and FALSE to '' with ifelse.

ind <- with(MyData, c(FALSE, farm[-1L]!= farm[-length(farm)]) & farm!='NULL')
MyData$switch <- ifelse(ind, 'New', '')

MyData
#   farm switch
#1     A       
#2     A       
#3  NULL       
#4     B    New
#5     B       
#6     B       
#7     A    New
#8     A       
#9     A       
#10    B    New
#11    B       
#12    B       
#13 NULL       
#14    A    New
#15    A       

To understand the concept of [-1L] and -length, suppose we have a vector

v1 <- c(2, 2, 3, 1, 5)
v1[-1] #removes the first observation
#[1] 2 3 1 5

v1[-length(v1)]# removes the last one
#[1] 2 2 3 1

When we compare these two, we are comparing the current row (v1[-length(v1)]) against the next row (v1[-1]). As the length is one less than the original length of 'v1', we append a 'TRUE' or 'FALSE' depending upon our logical condition

 c(FALSE, v1[-1]!= v1[-length(v1)])

In your case there is a second condition that asserts the value cannot be "NULL". So when combine both of this with &, only the TRUE values in both of them get the 'TRUE' and rest are 'FALSE'.

data

MyData <- structure(list(farm = c("A", "A", "NULL", "B", "B", "B", "A", 
"A", "A", "B", "B", "B", "NULL", "A", "A")), .Names = "farm",
class =  "data.frame", row.names = c(NA, -15L))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!