【问题标题】:Joint two datasets under restrictions in R在 R 的限制下联合两个数据集
【发布时间】:2021-08-03 23:16:25
【问题描述】:

我想根据月份和年份联合两个不同的数据集。每一个都有 3 列,一个是年份,一个是月份,最后一个是两个不同事物的平均值。第一个数据集有 2010 年和 2011 年,第二个数据集有 2015 年和 2016 年。我想制作第三个数据集,其中一列中有 2010 年 6 月的平均值,另一列中有 2015 年 10 月的平均值。换句话说,我想连接两个不同的年份和不同的月份。我希望它看起来像这样:

Year Month AVG
2010 October 15.7
2010 November 13.6
2010 December 13.9
Year Month AVG
2015 June 18.2
2015 July 18.4
2015 August 19.0
Year2 Month2 Year1 Month1 AVG2 AVG1
2015 June 2010 October 18.2 15.7
2015 July 2010 November 18.4 13.6
2015 August 2010 December 19.0 13.9

数据集 1 的一部分如下所示:

structure(list(Year = c(2010, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010), Month = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November"), Log_AVG = c(4.95582705760126, 4.86753445045558, 
5.34233425196481, 5.35185813347607, 5.33753807970132, 4.82028156560504, 
4.69134788222914, 5.29831736654804, 4.75359019110636, 5.12989871492307
)), row.names = c(NA, -10L), groups = structure(list(
    Year = c(2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 
    2010, 2010), Month = c("April", "August", "December", "February", 
    "January", "July", "June", "March", "May", "November"), .rows = structure(list(
        1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

第二个数据集的一部分看起来像这样:

structure(list(Year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015), Month = structure(1:10, .Label = c("January", "February", 
"March", "April", "May", "June", "July", "August", "September", 
"October", "November", "December"), class = c("ordered", "factor"
)), Log_AVG = c(0, 0, 9.08398318309966, 8.76029622047005, 
7.13089883029635, 7.07834157955767, 7.95892649305011, 8.8146275553107, 
9.69572510326022, 10.5731101880491)), row.names = c(NA, -10L), groups = structure(list(
     Year = 2015, .rows = structure(list(
        1:10), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

有什么方法可以用 R 做到这一点吗? 或者有什么方法可以按照我喜欢的方式对两个初始数据集进行排序(例如,保留一个原样,并根据月份而不是字母顺序对第二个数据集进行排序,将 2015 年 6 月作为第一个月并继续2015 年和 2016 年剩下的几个月)? 提前非常感谢!

【问题讨论】:

  • 由于您没有以特定方式排列数据,只需重命名列并将两个数据框与cbind()dplyr::bind_cols() 结合起来。
  • 这行得通,谢谢。不过还有一个问题。在我拥有的一个数据集中,月份的顺序不是我想要的正确顺序。我使用 spread 来创建使月份按字母顺序排列的数据集,这对我不起作用。有什么方法可以按照我想要的方式重新排序这个数据集?从六月开始,然后按照正确的顺序(六月,七月,八月......)所有其余月份?非常感谢
  • 使用内置的month.name 对象,该对象具有正确的月份名称。 Here's an example.

标签: r dplyr dataset


【解决方案1】:

如果您想要“date2”比“date1”早 5 年零 4 个月的固定偏移量,我会进行该计算。像这样的:

library(stringr)
library(lubridate)
df1 %>%
  mutate(
    date1 = ymd(paste(Year, Month, "01")),
    date2 = date1 + years(5) + months(4)
  ) %>%
  left_join(
    df2 %>% mutate(date2 = ymd(paste(Year, Month, "01"))),
    by = "date2",
    suffix = c("1", "2")
  )

如果您以复制/粘贴格式共享数据,我会很乐意进行测试和调试。

【讨论】:

  • 我刚刚上传了两个数据集的一部分。非常感谢!
  • 完美运行。谢谢,感谢您的帮助!
【解决方案2】:

听起来你只是想让 cbind 把两个数据框放在一起:

d1<-data.frame(Year=rep(2010,3),
               Month = c("October","November","December"),
               AVG=c(15.7,13.6,13.9))
d2<-data.frame(Year=rep(2015,3),
               Month = c("June","July","August"),
               AVG=c(15.7,13.6,13.9))
d3<-cbind(d1,d2)

这给了我们:

> d1
  Year    Month  AVG
1 2010  October 15.7
2 2010 November 13.6
3 2010 December 13.9
> d2
  Year  Month  AVG
1 2015   June 15.7
2 2015   July 13.6
3 2015 August 13.9
> d3<-cbind(d1,d2)
> d3
  Year    Month  AVG Year  Month  AVG
1 2010  October 15.7 2015   June 15.7
2 2010 November 13.6 2015   July 13.6
3 2010 December 13.9 2015 August 13.9

如果您必须按月排序,那么首先您需要转换为 month.name 的因子,如下所示:

d4<-data.frame(Year=rep(2010,3),
                   Month = c("November","December","October"),
                   AVG=c(13.6,13.9,15.7))
d4$Month <- factor(d4$Month, levels = month.name)
d4 <- d4[order(d4$Month),]

这给了我们这个:

> d4
  Year    Month  AVG
1 2010 November 13.6
2 2010 December 13.9
3 2010  October 15.7
> d4$Month <- factor(d4$Month, levels = month.name)
> d4 <- d4[order(d4$Month),]
> d4
  Year    Month  AVG
3 2010  October 15.7
1 2010 November 13.6
2 2010 December 13.9

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多