在R中的现有行之间添加新行数据
我有两个数据帧,我希望将它们连接在一起,这样在按降序排列数据之后,数据帧在R中的现有行之间添加新行数据,r,dataframe,merge,R,Dataframe,Merge,我有两个数据帧,我希望将它们连接在一起,这样在按降序排列数据之后,数据帧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
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
产品日期值
1A 2017-07-10 80
2A 2017-07-01 150
3B 2017-08-10 40
>df2
产品月价值
1990年7月1日A
2010年7月2日
3 B 8月30日
>结果
产品日期值
1A 2017-07-01 150
2A 2017-07-02 90
3A 2017-07-10 80
4 A 2017-07-11 50
5 B 2017-08-10 40
6b 2017-08-11 30
df一种方法是将df
中的日期增加1天,替换df
中df2
中的Value
,并绑定到原始数据帧
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
库(dplyr)
df$日期%
变异(日期=日期+1)%>%
安排(产品、日期)%>%
变异(值=df2%%>%RANGE(产品)%%>%pull(值))%%>%
绑定_行(df)%>%
安排(产品、日期)
#产品日期值
#1A 2017-07-01 150
#2A 2017-07-02 90
#3A 2017-07-10 80
#4 A 2017-07-11 50
#5 B 2017-08-10 40
#6b 2017-08-11 30
df
和df2
中的行数总是相同的?合并这些数据帧很困难,因为有冲突的列。您期望的结果显示一列“日期”,但无法从df2中的“七月”到结果中的2017-07-02。如果不是这样,我会简单地将它们与cbind结合起来,然后对它们进行排序。如果df2$Month[1]
是August
,那么预期的日期是什么?蔡达伦预计第二天发生在df@Ronak是的,df和df2中的行数始终相同。代码工作不正常,例如,尝试df$Value[1]在这种情况下,您的预期输出是什么?也许我误解了你的问题,因为在我的回答中,Value
列中的更改不会导致任何更改。我在问题中的代码后添加了不正确的解决方案。代码工作不正常,例如,请尝试df$Value[1],不幸的是没有。检查我的答案,看看问题中的那些值。你可以更具体一些。你是按降序说的。在第2行中,我将df按降序排列在“值”上。如果您想在“日期”下订单,可以使用以下命令:df=df[订单(df$Date,递减=T),]。如果您想按“日期”排序,而不是按降序排序,那么可以使用以下命令:df=df[order(df$Date),]
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$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