R-如何检查一行中的值是否与上一行中的值不同? [英] R - How can I check if a value in a row is different from the value in the previous row?

查看:103
本文介绍了R-如何检查一行中的值是否与上一行中的值不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表中添加一列,该列将值与现有列农场"中的先前值进行比较(以检查其是否相同);并且还控制当前行中的值是否为"NULL".目的是当该行的农场"列中的值与农场"列的上一行中的值不同时,在新的列切换"中取回值"new". (例如,当服务器场中的值为"NULL"时,我想取回")

I would like to add a column to my table that compares a value with a previous value in an existing column 'farm' (to check if it is the same); and also controls if the value in the current row is "NULL". The objective is to get back in the new column 'switch' the value "new" when the value in the column 'farm' for that row is different from the value in the previous row for the column 'farm'. (exept when the value in farm is "NULL", then I would like to get back "")

在此处查看所需的输出:

See here below the desired output:

farm    switch
A   
A   
NULL    
B   new
B   
B   
A   new
A   
A   
B   new
B   
B   
NULL    
A   new
A   

我尝试使用以下代码解决此问题:

I tried to solve this using the below code:

#To add a new column switch
MyData["switch"] <- NA

#To check if the value is different from the previous row; and if the value is different from NULL
MyData$switch <- ifelse((MyData$farm == lag(MyData$farm))||MyData$farm=="NULL","",MyData$farm)

但是当我使用此代码时,添加的列只有空值吗? 有人可以澄清我做错了什么,并通过可能有效的代码帮助我吗?

But when I use this code, then my added column has only empty values? Can somebody please clarify what I am doing wrong and help me with a code that might work?

推荐答案

我们通过将当前行与下一行进行比较来创建逻辑索引('ind')(我们可以通过删除(比较)农场"列),还包括元素不是"NULL"的条件.根据逻辑索引,可以使用ifelse将TRUE更改为'New',将FALSE更改为''.

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       

要了解[-1L]-length的概念,假设我们有一个向量

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

当我们比较这两个时,我们正在将当前行(v1[-length(v1)])与下一行(v1[-1])进行比较.由于该长度比原始长度"v1"小一,因此我们根据逻辑条件附加"TRUE"或"FALSE"

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)])

在您的情况下,还有第二个条件断言该值不能为"NULL".因此,当将这两个都与&结合使用时,它们两个中只有TRUE的值获得'TRUE',其余均为'FALSE'.

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'.

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))

这篇关于R-如何检查一行中的值是否与上一行中的值不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆