This question already has an answer here:
这个问题在这里已有答案:
- How to join (merge) data frames (inner, outer, left, right)? 13 answers
如何加入(合并)数据框(内部,外部,左侧,右侧)? 13个答案
I have two data frames with the same column names (shortterm and longterm). The data is also time series.
我有两个具有相同列名的数据框(短期和长期)。数据也是时间序列。
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)
I would like to extract the Austrian Data such that I have a new dataset that looks like
我想提取奥地利数据,以便我有一个看起来像的新数据集
Date shortterm longterm
2010-01-01 21000 23423
2010-04-01 23400 457
2010-01-07 26800 45457
What I've tried so far is to combine the data into a list
到目前为止我尝试过将数据合并到一个列表中
df.list <-list(shortterm.df,longterm.df)
and used lapply
并使用了lapply
setNames(do.call(cbind, lapply(df.list, `[`, 'Austria')), nm1)
But I would like this to be a data frame and I would like to keep the date element (which I've lost using this method)
但我希望这是一个数据框,我想保留日期元素(我使用此方法丢失了)
Any help would be greatly appreciated.
任何帮助将不胜感激。
3 个解决方案
#1
1
Here is one option using dplyr and tidyr.
这是使用dplyr和tidyr的一个选项。
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
#2
1
Another dplyr
and tidyr
possibility
另一种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
#3
0
Would something like that work?
会有类似的东西吗?
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)
Edit :
You could also use
你也可以使用
df <- cbind(Date,longterm.df$Austria,shortterm.df$Austria)
df <- as.data.frame(df)
Given the structure of your initial dataframes.
给出初始数据帧的结构。
#1
1
Here is one option using dplyr and tidyr.
这是使用dplyr和tidyr的一个选项。
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
#2
1
Another dplyr
and tidyr
possibility
另一种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
#3
0
Would something like that work?
会有类似的东西吗?
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)
Edit :
You could also use
你也可以使用
df <- cbind(Date,longterm.df$Austria,shortterm.df$Austria)
df <- as.data.frame(df)
Given the structure of your initial dataframes.
给出初始数据帧的结构。