简体   繁体   English

在R中,比较数据框中2行中的2个字段

[英]In R, comparing 2 fields across 2 rows in a dataframe

I am trying to compare 2 different fields across consecutive rows on a data frame in R and indicate the ones that are different. 我正在尝试比较R中数据帧上连续行中的2个不同字段,并指出不同的字段。 Below is the input data:- 以下是输入数据:

 Start    End
1 Atl      Bos    
2 Bos      Har  
3 Har      NYC  
4 Stf      SFO
5 SFO      Chi

I am trying to establish a chain of movement and where the End doesn't match up to the Start of the next row I want to indicate that row. 我正在尝试建立一个移动链,并且“结束”与下一行的“开始”不匹配,我想指示该行。 So for the above I would indicate row 4 as below:- 因此,对于上述内容,我将在第4行中指出以下内容:

 Start    End    Ind
1 Atl      Bos   Y 
2 Bos      Har   Y
3 Har      NYC   Y
4 Stf      SFO   N
5 SFO      Chi   Y

I am pretty new to R, I have tried looking up this problem but cant seem to find a solution. 我对R很陌生,我曾尝试查找此问题,但似乎找不到解决方案。 Any help is appreciated. 任何帮助表示赞赏。

An alternative would be: 一种替代方法是:

> Ind <- as.character(dat$Start[-1]) == as.character(dat$End [-length(dat$End)])
> dat$Ind <- c(NA, ifelse(Ind==TRUE, "Y", "N")) 
> dat
  Start End  Ind
1   Atl Bos <NA>
2   Bos Har    Y
3   Har NYC    Y
4   Stf SFO    N
5   SFO Chi    Y

Note that your first item should be <NA> 请注意,您的第一项应为<NA>

You can do that with dplyr using mutate and lead . 您可以使用mutatelead使用dplyr做到这dplyr Note that the last item should be NA because there is no line 6 to compare SFO-CHI to. 请注意,最后一项应为NA因为没有第6行可将SFO-CHI与之进行比较。

library(dplyr)
df1  <- read.table(text=" Start    End
Atl      Bos
Bos      Har
Har      NYC
Stf      SFO
SFO      Chi", header=TRUE, stringsAsFactors=FALSE)

df1 %>%
mutate(Ind=ifelse(End==lead(Start),"Y","N"))

  Start End  Ind
1   Atl Bos    Y
2   Bos Har    Y
3   Har NYC    N
4   Stf SFO    Y
5   SFO Chi <NA>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM