【问题标题】:Adding new rows of data between existing rows in R在 R 中的现有行之间添加新的数据行
【发布时间】:2020-07-31 09:32:44
【问题描述】:

我有两个数据框,我想将它们连接在一起,这样在按降序排列数据后,数据框df2 位于df 表的行之间。我还想在新添加的行中添加日期,以便每个日期都遵循日期的现有天数。

我的数据:

df
  Product       Date Value
1       A 2017-07-10    80
2       A 2017-07-01   150
3       B 2017-08-10    40
> df2
  Product  Month Value
1       A   July    90
2       A   July    50
3       B August    30

> result
  Product       Date Value
1       A 2017-07-01   150
2       A 2017-07-02    90
3       A 2017-07-10    80
4       A 2017-07-11    50
5       B 2017-08-10    40
6       B 2017-08-11    30


df <- data.frame(Product = c("A","A","B"),
                 Date = c("2017-07-10","2017-07-01","2017-08-10"),
                 Value =c(80,150,40))

df2 <- data.frame(Product = c("A","A","B"),
                 Month = c("July","July","August"),
                 Value =c(90,50,30))

不正确的解决方案:

df$Value[1] <- 500 ; df$Value[2] <- 50; df$Value[3] <- 400

Product       Date Value
1       A 2017-07-01    50
2       A 2017-07-02    90
3       A 2017-07-10   500
4       A 2017-07-11    50
5       B 2017-08-10   400
6       B 2017-08-11    30

Should be:

Product       Date Value
1       A 2017-07-01    50
2       A 2017-07-02    50
3       A 2017-07-10   500
4       A 2017-07-11    90
5       B 2017-08-10   400
6       B 2017-08-11    30

【问题讨论】:

  • dfdf2 中的行数总是相同?
  • 合并这些数据框很棘手,因为您有冲突的列。您想要的结果显示一列“日期”,但无法从 df2 中的“七月”到结果中的2017-07-02。如果不是这样,我会简单地将它们与 cbind 结合起来,然后对它们进行排序。
  • 如果df2$Month[1]August,预计日期是多少?
  • @Darren Tsai 我预计第二天会在 df 日期之后发生
  • @Ronak Shah 是的,df 和 df2 中的行数始终相同。

标签: r dataframe merge


【解决方案1】:

一种方法是将df 中的日期增加1 天,将Value 替换为df 中的df2 并绑定到原始数​​据框。

library(dplyr)  

df$Date <- as.Date(df$Date)

df %>%
  mutate(Date = Date + 1) %>%
  arrange(Product, Date) %>%
  mutate(Value = df2 %>% arrange(Product) %>%  pull(Value)) %>%
  bind_rows(df) %>%
  arrange(Product, Date)

#  Product       Date Value
#1       A 2017-07-01   150
#2       A 2017-07-02    90
#3       A 2017-07-10    80
#4       A 2017-07-11    50
#5       B 2017-08-10    40
#6       B 2017-08-11    30

【讨论】:

  • 代码运行不正常,例如试试df$Value[1] &lt;- 500 ; df$Value[2] &lt;- 50; df$Value[3] &lt;- 400
  • 您对这种情况的预期输出是什么?也许我误解了你的问题,因为在我的回答中,Value 列的变化没有任何变化。
  • 我在我的问题中添加了错误的解决方案。
【解决方案2】:
df$Date = as.Date(df$Date)
df = df[order(df$Value, decreasing=T), ]

nr = nrow(df) * 2
result = data.frame(Product = rep('', nr), Date = rep(NA, nr), Value = rep(NA, nr))

idx = c(1:nr)
result[which(idx %% 2 == 1), ] = df

df2 = data.frame(Product = df2$Product, Date = as.Date(df$Date, '%Y-%m-%d') + 1, Value = df2$Value)
result[which(idx %% 2 == 0), ] = df2

result$Date = as.Date(result$Date, origin = "1970-01-01")

> result
  Product       Date Value
1       A 2017-07-10   500
2       A 2017-07-11    90
3       B 2017-08-10   400
4       A 2017-08-11    50
5       A 2017-07-01    50
6       B 2017-07-02    30

【讨论】:

  • 代码运行不正常,例如试试df$Value[1] &lt;- 500 ; df$Value[2] &lt;- 50; df$Value[3] &lt;- 400
  • 很遗憾没有。检查我对问题中这些值的回答。
  • 你可以更具体。你说的是“降序”。在第 2 行中,我按降序对“值”中的 df 进行排序。如果您想在“日期”订购它,您可以使用:df = df[order(df$Date, reduction=T), ]。如果你想在“日期”排序,但不是降序,那么你可以使用这个:df = df[order(df$Date), ]