简体   繁体   English

R将最后一行附加到数据帧

[英]R append last row to a data frame

I have a data frame (df) that shares a key column ($Name) with a list of data frames: 我有一个数据框(df),它与数据框列表共享一个键列($ Name):

head(df)
# A tibble: 6 x 3 ##truncating to show first 2 rows only
  Name      var1  var2
 <chr>      <chr> <chr>
1 Tom Marks LAX   ORD
2 Bob Sells MIA   CHI

I have a list of data frames that contains historical data for each person contained in df$Name. 我有一个数据框列表,其中包含df $ Name中每个人的历史数据。

head(employees$'Tom Marks')
Name      date       var3
Tom Marks 2017-01-01 250
Tom Marks 2017-01-02 457

head(employees$'Bob Sells')
Name      date       var3
Bob Sells 2017-01-01 385
Bob Sells 2017-01-02 273

I would like to append the value in var3 from employees list to the df by the most recent date (which is always the last row in an employees list). 我想在var3中将雇员列表中的值附加到最新日期之前(这始终是雇员列表中的最后一行)。 For example, the output, after matching Tom Marks from df$Name to employees$'Tom Marks' would look like this: 例如,输出,从DF $名称匹配汤姆商标员工$“汤姆商标”后是这样的:

head(df)
  Name      var1  var2  var3
 <chr>      <chr> <chr> <num>
1 Tom Marks LAX   ORD   457
2 Bob Sells MIA   CHI   273

I have spent a decent amount of time researching filtering joins, mutating joins, bind_rows, reduce() functions but have been unsuccessful in accomplishing what is probably an easy task for a decent programmer. 我花了大量的时间研究过滤联接,变异联接,bind_rows,reduce()函数,但未能成功完成对体面的程序员来说可能是一件容易的事情。 I'm hoping someone out there can put me out of my misery and provide some better direction or better yet, an answer! 我希望外面有人可以使我摆脱困境,并提供更好的指导或更好的答案!

Thank you! 谢谢!

If you're always after the last row, you can use tail to get it: 如果您总是在最后一行之后,可以使用tail来获取它:

library(tidyverse)
left_join(
  df, 
  map_df(employees, ~ tail(.x, 1))  
)

This solution relies on the fact that your data arranged as you said they were, but you can easily arrange the list by date if they were not so. 此解决方案基于您的数据按您所说的那样排列的事实,但是如果您不按date排列,则可以轻松地按date排列列表。

library(tidyverse)
df %>% left_join(
  df_list$employees %>% 
    bind_rows() %>% 
    group_by(Name) %>% 
    summarise_at(vars(var3), last))

#        Name var1 var2 var3
# 1 Tom Marks  LAX  ORD  457
# 2 Bob Sells  MIA  CHI  273

Data 数据

df <- data.frame(Name = c("Tom Marks", "Bob Sells"),
                 var1 = c("LAX", "MIA"),
                 var2 = c("ORD", "CHI"))

df_list <- list(employees = list(
  `Tom Marks` = data.frame(Name = "Tom Marks",
                           date = c("2017-01-01", "2017-01-02"),
                           var3 = c(250, 457)),
  `Bob Sells` = data.frame(Name = "Bob Sells",
                           date = c("2017-01-01", "2017-01-02"),
                           var3 = c(385, 273))
))

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

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