合并来自具有唯一名称的不同数据框的列

时间:2018-07-07 13:31:58

标签: r dataframe

我有以下三个data.frames

我正在尝试合并列"ticker".Close中的列,其中每个“行情指示器”对应于列表tickers中的名称。 data.frames也由其各自的代码进行调用。所以我试图得到一个最终的数据帧:

df:

IFF.Close | LYB.Close | NEM.close
1234          5678        8888
4545          8789        8987

由于股票行情发生变化,我正在尝试使之自动化。

列表:

  tickers <- c("IFF", "LYB", "NEM")

数据:

IFF <- structure(c(153.039993, 154.929993, 156.279999, 154.460007, 156.839996, 
155.929993, 154.559998, 156.589996, 157.220001, 156.929993, 156.839996, 
156.490005, 152.539993, 154.529999, 153.589996, 154.320007, 155.449997, 
154.139999, 154.559998, 155.740005, 153.759995, 156.869995, 155.830002, 
154.259995, 264500, 243900, 371300, 259900, 208000, 214200, 152.908661, 
154.076065, 152.117203, 155.19397, 154.165085, 152.611862), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "yahoo", updated = structure(1530969585.64543, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1514851200, 
1514937600, 1515024000, 1515110400, 1515369600, 1515456000), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
6L), .Dimnames = list(NULL, c("IFF.Open", "IFF.High", "IFF.Low", 
"IFF.Close", "IFF.Volume", "IFF.Adjusted")))

和;

LYB <- structure(c(111.050003, 111.82, 112.800003, 113.25, 114.400002, 
114.330002, 112.260002, 112.419998, 113.440002, 114.459999, 114.400002, 
116.339996, 110.690002, 110.699997, 111.809998, 112.330002, 113.040001, 
114.209999, 112.230003, 112.260002, 112.800003, 114.370003, 114.110001, 
115.150002, 1127800, 1437300, 1631000, 1082000, 1283200, 1794000, 
110.254074, 110.283546, 110.814034, 112.356392, 112.100967, 113.122658
), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "yahoo", updated = structure(1530969586.76173, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1514851200, 
1514937600, 1515024000, 1515110400, 1515369600, 1515456000), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
6L), .Dimnames = list(NULL, c("LYB.Open", "LYB.High", "LYB.Low", 
"LYB.Close", "LYB.Volume", "LYB.Adjusted")))

和;

NEM <- structure(c(37.869999, 38.209999, 37.75, 38.18, 38.349998, 38.150002, 
38.299999, 38.259998, 38.259998, 38.43, 38.52, 38.32, 37.82, 
37.490002, 37.369999, 38.02, 37.959999, 37.950001, 38.16, 37.84, 
38.259998, 38.400002, 38.360001, 38.110001, 5325800, 5143600, 
3876000, 2877300, 4719800, 3902600, 37.884403, 37.566715, 37.983677, 
38.122669, 38.082958, 37.834763), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "yahoo", updated = structure(1530969590.28438, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1514851200, 
1514937600, 1515024000, 1515110400, 1515369600, 1515456000), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
6L), .Dimnames = list(NULL, c("NEM.Open", "NEM.High", "NEM.Low", 
"NEM.Close", "NEM.Volume", "NEM.Adjusted")))

编辑:这是我拥有的代码,可带您进入自己所在的位置:

library(rvest)
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
SP500 <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[1]') %>%
  html_table()
SP500 <- SP500[[1]]
Tix <- SP500$`Ticker symbol`
Tix


industries <- unique(SP500$`GICS Sub Industry`)
library(dplyr)
industries <- industries %>%
  data.frame() %>%
  arrange(industries)

myfirms <- SP500[SP500$`GICS Sub Industry` == "Asset Management & Custody Banks", ]

unique(SP500$`GICS Sector`)

sectors <- unique(SP500$`GICS Sector`)
library(dplyr)
sectors <- sectors %>%
  data.frame() %>%
  arrange(sectors)

myfirms <- SP500[SP500$`GICS Sector` == "Materials", ]
myTixsectors <- myfirms$`Ticker symbol`


library(quantmod)
getSymbols(myTixsectors, from = "2018-01-01", to = "2018-06-01")

该代码基本上从Wikipedia中获取SP500公司的列表,我按行业和行业对其进行了细分。我使用Quantmod软件包下载针对每个公司股价调整的开盘价,收盘价,最高价,最低价。我现在想做的就是用每个公司子集(按部门划分)的收盘价制作一个data.frame

0 个答案:

没有答案