简体   繁体   English

使用 R 在不同长度的列之间映射和赋值

[英]Mapping and assigning values between columns of different lengths using R

I got a df such as this我有一个像这样的df

structure(list(id = c(4375, 4375, 4375, 4375), time = c(0, 88, 
96, 114)), class = "data.frame", row.names = c(NA, -4L))

and a second df (df2) such as this和第二个 df (df2) 像这样

structure(list(id2 = c(4375, 4375, 4375, 4375, 4375, 4375, 4375, 
4375, 4375, 4375), time2 = c(0, 2, 87, 88, 94, 97, 101, 104, 
109, 114), score2 = c(0.028, 0.057, 0.057, 0.057, 0.057, 0.057, 
0.057, 0.085, 0.085, 0.085)), class = "data.frame", row.names = c(NA, 
-10L))

I want for each id, to map the time column in both df and creating a score column in df1 and assigning the value of score from df2, when the time value in df1 is equal or less than the time in df2.当df1中的时间值等于或小于df2中的时间时,我希望对于每个id,map df中的时间列和在df1中创建一个分数列并从df2分配分数值。 I want the final df to look like this我希望最终的 df 看起来像这样

structure(list(id3 = c(4375, 4375, 4375, 4375), time3 = c(0, 
88, 96, 116), score3 = c(0.028, 0.057, 0.057, 0.085)), class = "data.frame", row.names = c(NA, 
-4L))

For exact matches in the id and time columns, we can use left_join :对于 id 和 time 列中的完全匹配,我们可以使用left_join

library(dplyr)

left_join(df, df2, by=c('id'='id2', 'time'='time2'))

    id time score2
1 4375    0  0.028
2 4375   88  0.057
3 4375   96     NA
4 4375  114  0.085

However, as we can see, this wont handle near-matches well.但是,正如我们所见,这不能很好地处理近场比赛。 Please define, what do you mean by " when the time value in df1 is equal or less than the time in df2.".请定义,“当 df1 中的时间值等于或小于 df2 中的时间时”是什么意思。 equal or less than what?等于或小于什么? How do we match the elements for the "equal or less then" comparison?我们如何匹配“等于或小于”比较的元素?

library(fuzzyjoin)
fuzzy_left_join(df, df2, by = c("time"="time2"), match_fun = list(`<=`)) %>% 
  group_by(time2) %>% 
  slice(n()) %>% 
  ungroup() %>% 
  group_by(time) %>% 
  filter(row_number()==1) %>% 
  select(id3=id, time3=time, score3 = score2)
    id3 time3 score3
  <dbl> <dbl>  <dbl>
1  4375     0  0.028
2  4375    88  0.057
3  4375    96  0.057
4  4375   114  0.085

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

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