简体   繁体   English

如何获得先前的匹配值

[英]how to get previous matching value

how to get value of "a" if value of b matches with most recent previous value eg - row$3 of b matches with previous row$1 ,row$6 matches with row$4 如果b的值与最近的先前值匹配,如何获取“ a”的值,例如-b的row $ 3与先前的row $ 1匹配,row $ 6与row $ 4匹配

df <- data.frame(year = c(2013,2013,2014,2014,2014,2015,2015,2015,2016,2016,2016),
           a = c(10,11,NA,13,22,NA,19,NA,10,15,NA),
           b = c(30.133,29,30.1223,33,17,33,11,17,14,13.913,14))

  year  a   b   *NEW*
2013    10  30.133  NA
2013    11  29      NA
2014    NA  30.1223 10
2014    13  33      NA
2014    22  17      NA
2015    NA  33      13
2015    19  11      NA
2015    NA  17      22
2016    10  14      NA
2016    15  13.913  10
2016    NA  14      15

Thanks 谢谢

For OPs example case 对于OP示例案例

One way could be is to use duplicated() function. 一种方法是使用duplicated()函数。

# Input dataframe    
df <- data.frame(year = c(2013,2013,2014,2014,2014,2015,2015,2015,2016,2016,2016),
                     a = c(10,11,NA,13,22,NA,19,NA,10,15,NA),
                     b = c(30,29,30,33,17,33,11,17,14,14,14))

# creating a new column with default values
df$NEW <- NA

# updating the value using the previous matching position
df$NEW[duplicated(df$b)] <- df$a[duplicated(df$b,fromLast = TRUE)]

# expected output
df
#    year  a  b NEW
# 1  2013 10 30  NA
# 2  2013 11 29  NA
# 3  2014 NA 30  10
# 4  2014 13 33  NA
# 5  2014 22 17  NA
# 6  2015 NA 33  13
# 7  2015 19 11  NA
# 8  2015 NA 17  22
# 9  2016 10 14  NA
# 10 2016 15 14  10
# 11 2016 NA 14  15

General purpose usage 通用用途

The above solution fails when the duplicates are not in sequential order. 当重复项不是按顺序排列时,上述解决方案将失败。 As per @DavidArenburg's advice. 按照@DavidArenburg的建议。 I have changed the fourth element df$b[4] <- 14 . 我更改了第四个元素df$b[4] <- 14 The general solution would require the usage of another handy function order() and should work for different possible cases. 通用解决方案将需要使用另一个方便的函数order()并且应该适用于不同的可能情况。

# Input dataframe    
df <- data.frame(year = c(2013,2013,2014,2014,2014,2015,2015,2015,2016,2016,2016),
                 a = c(10,11,NA,13,22,NA,19,NA,10,15,NA),
                 b = c(30,29,30,14,17,33,11,17,14,14,14))

# creating a new column with default values
df$NEW <- NA

# sort the matching column
df <- df[order(df$b),]

# updating the value using the previous matching position
df$NEW[duplicated(df$b)] <- df$a[duplicated(df$b,fromLast = TRUE)]

# To original order
df <- df[order(as.integer(rownames(df))),]

# expected output
df
#    year  a  b NEW
# 1  2013 10 30  NA
# 2  2013 11 29  NA
# 3  2014 NA 30  10
# 4  2014 13 14  NA
# 5  2014 22 17  NA
# 6  2015 NA 33  NA
# 7  2015 19 11  NA
# 8  2015 NA 17  22
# 9  2016 10 14  13
# 10 2016 15 14  10
# 11 2016 NA 14  15

Here, the solution is based on the base package' functions. 在此,解决方案基于base软件包的功能。 I am sure there should other ways of doing this using other packages. 我确信应该有其他使用其他软件包的方法。

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

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