Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用多个数据帧中的列创建新数据帧_R_Dataframe_Dplyr_Tidyr - Fatal编程技术网

R 使用多个数据帧中的列创建新数据帧

R 使用多个数据帧中的列创建新数据帧,r,dataframe,dplyr,tidyr,R,Dataframe,Dplyr,Tidyr,我有两个列名称相同的数据帧(短期和长期)。数据也是时间序列 Austria<-c(21000, 23400, 26800) Aruba<-c(50282, 234934, 34634) Date<- as.Date(c('2010-01-01','2010-04-01','2010-01-07')) shortterm.df <- data.frame(Date,Austria, Aruba) Austria <- c(23423, 457, 45457)

我有两个列名称相同的数据帧(短期和长期)。数据也是时间序列

Austria<-c(21000, 23400, 26800)
Aruba<-c(50282, 234934, 34634)
Date<- as.Date(c('2010-01-01','2010-04-01','2010-01-07'))
shortterm.df <- data.frame(Date,Austria, Aruba)


 Austria <- c(23423, 457, 45457)
 Aruba <- c(7768, 67679, 67979)
 Date <- as.Date(c('2010-01-01','2010-04-01','2010-01-07'))
longterm.df <- data.frame(Date,Austria, Aruba)
到目前为止,我尝试的是将数据合并到一个列表中

df.list <-list(shortterm.df,longterm.df)
但我希望这是一个数据帧,我希望保留date元素(使用此方法时丢失了该元素)


任何帮助都将不胜感激

你喜欢那样的工作吗

Austria.short <- c(21000, 23400, 26800)
Austria.long <- c(23423, 457, 45457)
Date <- as.Date(c('2010-01-01','2010-04-01','2010-01-07'))

df <- cbind(Date,Austria.long,Austria.short)
df <- as.data.frame(df)

Austria.short这样行吗

Austria.short <- c(21000, 23400, 26800)
Austria.long <- c(23423, 457, 45457)
Date <- as.Date(c('2010-01-01','2010-04-01','2010-01-07'))

df <- cbind(Date,Austria.long,Austria.short)
df <- as.data.frame(df)
Austria.short这里有一个使用and的选项

库(dplyr)
图书馆(tidyr)
dat%
集合名(c(“短期”、“长期”))%>%
绑定行(.id=“Data”)%>%
选择(-Aruba)%>%
传播(数据,奥地利)
dat
#日期长期短期
# 1 2010-01-01    23423     21000
# 2 2010-01-07    45457     26800
# 3 2010-04-01      457     23400
这里有一个使用and的选项

库(dplyr)
图书馆(tidyr)
dat%
集合名(c(“短期”、“长期”))%>%
绑定行(.id=“Data”)%>%
选择(-Aruba)%>%
传播(数据,奥地利)
dat
#日期长期短期
# 1 2010-01-01    23423     21000
# 2 2010-01-07    45457     26800
# 3 2010-04-01      457     23400

另一个
dplyr
tidyr
可能性

library(tidyr)
library(dplyr)

inner_join(gather(shortterm.df, key = country, value = shortterm, -Date),
           gather(longterm.df, key = country, value = longterm, -Date)) %>% 
  filter(country == "Austria")

Joining, by = c("Date", "country")
        Date country shortterm longterm
1 2010-01-01 Austria     21000    23423
2 2010-04-01 Austria     23400      457
3 2010-01-07 Austria     26800    45457

另一个
dplyr
tidyr
可能性

library(tidyr)
library(dplyr)

inner_join(gather(shortterm.df, key = country, value = shortterm, -Date),
           gather(longterm.df, key = country, value = longterm, -Date)) %>% 
  filter(country == "Austria")

Joining, by = c("Date", "country")
        Date country shortterm longterm
1 2010-01-01 Austria     21000    23423
2 2010-04-01 Austria     23400      457
3 2010-01-07 Austria     26800    45457

将列重命名为
短期
长期
,然后使用
合并
。将列重命名为
短期
长期
,然后使用
合并
。非常感谢!这真是一次享受。非常感谢!这是一种享受。
library(dplyr)
library(tidyr)

dat <- list(shortterm.df, longterm.df) %>%
  setNames(c("shortterm", "longterm")) %>%
  bind_rows(.id = "Data") %>%
  select(-Aruba) %>%
  spread(Data, Austria)
dat
#         Date longterm shortterm
# 1 2010-01-01    23423     21000
# 2 2010-01-07    45457     26800
# 3 2010-04-01      457     23400
library(tidyr)
library(dplyr)

inner_join(gather(shortterm.df, key = country, value = shortterm, -Date),
           gather(longterm.df, key = country, value = longterm, -Date)) %>% 
  filter(country == "Austria")

Joining, by = c("Date", "country")
        Date country shortterm longterm
1 2010-01-01 Austria     21000    23423
2 2010-04-01 Austria     23400      457
3 2010-01-07 Austria     26800    45457