Retrieve Stock Price - r

I'm trying to retrieve stock prices with the following code using quantmod. The code below allows me to retrieve what I need. However, when I export the CSV file, the date column (first column) appears only as a sequence of numbers. To be clear, it's fine in R when I open a data frame, but changes when exported.
from.dat <- as.Date("01/01/12", format="%m/%d/%y")
to.dat <- as.Date("12/31/17", format="%m/%d/%y")
getSymbols("GOOG", src="yahoo", from = from.dat, to = to.dat)
write.csv(GOOG, file = "Googletest.csv", row.names = TRUE)
Any ideas how to get the code to include a date? - Mike

This seems like a duplicate from this one. The problem boils down to the fact your file is a xts object. Either way, just replace the last line by
write.csv(as.data.frame(GOOG), file = "Googletest.csv", row.names = TRUE)
and you'll be fine.

Related

Converting dataframe into csv with "unexpected token"

I am currently writing a code to download timeseries (which will then be converted into csv-files) to conduct an event study upon.
The following code (part of the complete code) I wrote:
tickers = c("^AEX", "^ATX", "^BFX", "^FCHI", "^FTSE", "^GDAXI", "^IBEX", "^OMX","^OMXH25", "^OSEAX", "^SSMI", "FTSEMIB.MI")
Aggregate <- getSymbols(tickers,
from = "2014-01-01",
to = "2021-12-31")
na.omit(Aggregate,"iz",interp="linear")
Ticker <- Aggregate
Ticker
class(Ticker)
data1 <-as.data.frame(Ticker)
data1
class(data1)
data2 <- data1 # Duplicate data frame
data2 # Print new data frame
AEX <- ^AEX
write.zoo(AEX,"//Users/TEST/Library/CloudStorage/OneDrive-Personal/Event Study Basis\\AEX.csv",index.name="Date",sep=",")
As the tickers of the indices (^AEX, ^ATX etc.) all possess a "^", which Excel doesn't "eat" I want to make sure the dataframe I want to export to a .csv file does not possess this "^". For a different analysis, the code worked (different tickers) now I get an error every time I try to run it.
My questions:
which command will solve my problem? --> Converting ^AEX into AEX so Excel eats it :)

R - Export large dataframe into CSV

Beginner here: I have a list (see screenshot) called Coins_list from which I want to export the second dataframe stored in it called data into a csv. When I use the code
write.csv(Coins_list$data, file = "Coins_list_full_data.csv")
I get a huge CSV with a bunch of numbers from the column named price which apparently containts more dataframes, if I read the output correctly or at least display the data in the price column? How can I export this dataframe into CSV correctly? See screenshot for more details.
EDIT: I was able to get the first four rows into CSV by using df2 <- Coins_list$data write.csv(df2[1:4,], file="BTC_row.csv"), however it now looks like R puts the price of all four rows within a list c( ) and repeats it in each row? Any idea how to change that?
(I would post this as a comment but I have too few reputation)
Hey, you could try for starters to flatten the json file by going further than response list$content but looking at what's into the content with another $.
Else you could try getting data$price and see what pops up from there.
something like this:
names = list(data$symbol)
df = data.frame(price = NA, symbol = NA)
for (i in length(data)) {
x = data.frame(price = data$price[i], symbol = names[i])
df = inner_join(df, data)
}
to get a dataframe with price and symbol. I don't know how the data is nested so I'm just guessing.
It would be helpful to know from where you got the data for reproducibility.

Date formatted cell in xlsx files to R

I have an excel file which has date information in some cells. like :
I read this file into R by the following command :
library(xlsx)
data.files = list.files(pattern = "*.xlsx")
data <- lapply(data.files, function(x) read.xlsx(x, sheetIndex = 9,header = T))
Everything is correct except the cells with date! instead of having the xlsx information into those cell, I always have 42948 as a date :
Does anybody know how could I fix this ?
As you can see, after importing your files, dates are represented as numeric values (here 42948). They are actually the internal representation of the date information in Excel. Those values are the ones that R presents instead of the “real” dates.
You can get those dates in R with as.Date(42948 - 25569, origin = "1970-01-01")
Notice that you can also use a vector containing the internal representation of the dates, so this should also work
vect <- c(42948, 42949, 42950)
as.Date(vect - 25569, origin = "1970-01-01")
PS: To convert an Excel datetime colum, see this (p.31)

Changing the name of a dataframe with an = sign in it

My question is regarding changing the name of a dataframe that I imported using the quantmod package. I ran the following lines,
library(quantmod)
data <- getSymbols("GBP=x", from = "2013-01-01", to = "2017-06-01", src="yahoo")
Which then saved the data as GBP=x
I now want to change the name of this dataframe to something called "GBP".
I keep getting values and not a dataframe.
GBP GBP=x
When I run GBP <- as.data.frame('GBP=x') I just get a dataframe with the value of GBP=x - 1 observation of 1 variable.
Any help is much appreciated
(Alternatively if you can suggest a way to download FX data from quantmod storing it as a more convenient name that would do the trick also.
If I understand the documentation correctly,
data <- getSymbols("GBP=x", from = "2013-01-01", to = "2017-06-01", src="yahoo",auto.assign=FALSE)
will result in the FX data being stored in data.
Also, in case you have trouble finding the ` key, it's on the top left of most keyboards. It's used generally in R to enclose troublesome characters.
You need to use '`':
GBP = `GBP=X`
# remove the original dataframe from your workspace
rm(`GBP=X`)

Data transpose function in R not working properly

I am using R to do some work but I'm having difficulties in transposing data.
My data is in rows and the columns are different variables. When using the function phyDat, the author indicates a transpose function because importing data is stored in columns.
So I use the following code to finish this process:
#read file from local disk in csv format. this format can be generated by save as function of excel.
origin <- read.csv(file.choose(),header = TRUE, row.names = 1)
origin <- t(origin)
events <- phyDat(origin, type="USER", levels=c(0,1))
When I check the data shown in R studio, it is transposed but the result it is not. So I went back and modified the code as follows:
origin <- read.csv(file.choose(),header = TRUE, row.names = 1)
events <- phyDat(origin, type="USER", levels=c(0,1))
This time the data does not reflect transposed data, and the result is consistent with it.
How I currently solve the problem is transposing the data in CSV file before importing to R. Is there something I can do to fix this problem?
I had the same problem and I solved it by doing an extra step as follows:
#read file from local disk in csv format. this format can be generated by save as function of excel.
origin <- read.csv(file.choose(),header = TRUE, row.names = 1)
origin <- as.data.frame(t(origin))
events <- phyDat(origin, type="USER", levels=c(0,1))
Maybe it is too late but hope it could help other users with the same problem.

Resources