my dataset is missing name for the first column (there are dates in it)
I tried colnames(managers)[1] <- "date" but it renamed the second column
> #load data
> data(managers)
> colnames(managers)[1] <- "date"
> View(head(managers,10))
> str(managers)
An ‘xts’ object on 1996-01-31/2006-12-31 containing:
Data: num [1:132, 1:10] 0.0074 0.0193 0.0155 -0.0091 0.0076 -0.0039 -0.0231 0.0395 0.0147 0.0288 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:10] "date" "HAM2" "HAM3" "HAM4" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
dataset headers
The 'managers' is an xts object and the dates are the index
library(PerformanceAnalytics)
index(managers)
#[1] "1996-01-31" "1996-02-29" "1996-03-31" "1996-04-30" "1996-05-31" "1996-06-30" ...
The columns of the dataset are
colnames(managers)
#[1] "HAM1" "HAM2" "HAM3" "HAM4" "HAM5" "HAM6" "EDHEC LS EQ" "SP500 TR" "US 10Y TR" "US 3m TR"
If we want to convert it to data.frame, then use fortify.zoo
library(zoo)
managers1 <- fortify.zoo(managers)
colnames(managers)[1] <- 'date'
Or specify the names in fortify.zoo
managers1 <- fortify.zoo(managers, names = "date")
Related
I want to create an arraylist of the price data of certain stocks.
First, I selected my basket of stocks using:
tickers <- c("^GSPC","MSFT","INTC","NVDA","AAPL")
Next, I downloaded the price data using a for loop function:
for (i in 1:length(tickers)) {
getSymbols(tickers[i],
from = as.Date("2006-01-01"), to = as.Date("2009-12-31"))
}
Now, I want to add each stock data into an arraylist, so I tried something like this:
s <- list()
for (i in 1:length(tickers)) {
getSymbols(tickers[i],
from = as.Date("2006-01-01"), to = as.Date("2009-12-31")) %>%
{. ->> s[[i]]}
}
But the output seems to only give me an arraylist of the name of the stocks:
[[1]] [1] "GSPC"
[[2]] [1] "MSFT"
[[3]] [1] "INTC"
[[4]] [1] "NVDA"
[[5]] [1] "AAPL"
Is there something wrong with the code I gave after the pipe function?
Just use lapply to create your list object and make sure to set the option auto.assign to FALSE.
library(quantmod)
tickers <- c("^GSPC","MSFT","INTC","NVDA","AAPL")
# Get the ticker data
s <- lapply(tickers, getSymbols, from = as.Date("2006-01-01"), to = as.Date("2009-12-31"), auto.assign = FALSE)
# name the list objects
names(s) <- tickers
str(s)
List of 5
$ ^GSPC:An ‘xts’ object on 2006-01-03/2009-12-30 containing:
Data: num [1:1006, 1:6] 1248 1269 1273 1273 1285 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "GSPC.Open" "GSPC.High" "GSPC.Low" "GSPC.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
..$ src : chr "yahoo"
..$ updated: POSIXct[1:1], format: "2018-12-07 15:01:48"
$ MSFT :An ‘xts’ object on 2006-01-03/2009-12-30 containing:
.....
I'm trying to operate on a specific column in an xts object by name within a function but I keep getting an error:
Error in if (length(c(year, month, day, hour, min, sec)) == 6 && all(c(year, :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In as_numeric(YYYY) : NAs introduced by coercion
2: In as_numeric(YYYY) : NAs introduced by coercion
If I have an xts object:
xts1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
xts2 <- xts(x=1:10, order.by=Sys.Date()+1:10)
xts3 <- merge(xts1, xts2)
Then I can select a specific column with:
xts3$xts1
With a dataframe I can pass xts3 to another function and then select a specific column with:
xts3['xts1']
But if I try to do the same thing with an xts object I get the error above. e.g.
testfun <- function(xts_data){
print(xts_data['xts1'])
}
Called with:
testfun(xts3)
This works:
testfun <- function(xts_data){
print(xts_data[,1])
}
But I'd really like to select by name as I can't be certain of the column order.
Can anyone suggest how to solve this?
Thanks!
xts-objects have class c("xts", "zoo"), which means they are matrices with special attributes that are assigned by their creation functions. Although $ will not succeed with a matrix, it works with xts and zoo objects thanks to the $.zoo method. (It's also not recommended to use $ inside functions because of the potential for name-evaluation-confusion and partial name matching.) See: ?xts and examine the sample.xts object created with the first example with str:
> ?xts
starting httpd help server ... done
> data(sample_matrix)
> sample.xts <- as.xts(sample_matrix, descr='my new xts object')
>
> str(sample.xts)
An ‘xts’ object on 2007-01-02/2007-06-30 containing:
Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
List of 1
$ descr: chr "my new xts object"
class(sample.xts)
# [1] "xts" "zoo"
This explains why the earlier answer advising the use of xts3[ , "x"] or equivalently xts3[ , 1] should succeed. The [.xts function extracts the "Data" element first and then returns the either named or numbered column specified by the j-argument.
str(xts3)
An ‘xts’ object on 2018-05-24/2018-06-13 containing:
Data: int [1:20, 1:2] 10 9 8 7 6 5 4 3 2 1 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "xts1" "xts2"
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
> xts3[ , "xts1"]
xts1
2018-05-24 10
2018-05-25 9
2018-05-26 8
2018-05-27 7
2018-05-28 6
2018-05-29 5
2018-05-30 4
2018-05-31 3
2018-06-01 2
2018-06-02 1
2018-06-04 NA
2018-06-05 NA
2018-06-06 NA
2018-06-07 NA
2018-06-08 NA
2018-06-09 NA
2018-06-10 NA
2018-06-11 NA
2018-06-12 NA
2018-06-13 NA
The merge.xts operation might not have delivered what you expected since the date ranges didn't overlap. It seems possible that you wanted:
> xts4 <- rbind(xts1, xts2)
> str(xts4)
An ‘xts’ object on 2018-05-24/2018-06-13 containing:
Data: int [1:20, 1] 10 9 8 7 6 5 4 3 2 1 ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
Note that the rbind.xts-operation failed to deliver an object with the shared column name so numeric access would be needed. (I would have expected a named "Data" element, but you/we also need to read ?rbind.xts.)
Type ?`[.xts` and you'll see that the function has a i and a j argument (among others).
i - the rows to extract. Numeric, timeBased or ISO-8601 style (see details)
j - the columns to extract, numeric or by name
You passed 'xts1' as the i argument, while it should be j. So your function should be
testfun <- function(xts_data){
print(xts_data[, 'xts1']) # or xts3[j = 'xts1']
}
I enter a headed Excel CSV and examine with str(returns.xts). The following code generates character values within the xts.
file <- "~/GCS/returns_Q216.csv"
returns_Q216_ <- read.csv(file=file)
returns <- read.zoo(data.frame(returns_Q216_), FUN = as.Date, format='%d/%m/%Y')
returns.xts <- as.xts(returns)
What is the best way to convert the xts contents to numeric from character whilst preserving xts (and date column)?
> `str(returns)`
An ‘xts’ object on 2007-01-31/2015-05-31 containing:
Data: `chr` [1:101, 1:18] "-0.002535663" "-0.001687755" "0.032882512" "0.024199512" "0.027812955" ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:18] "UK.EQUITY" "EUR.EQUITY" "NA.EQUITY" "ASIA.EQUITY" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
NULL
> returns[8,9]
PROPERTY
2007-08-31 "-4.25063E-05"
When I try as.numeric(returns.xts) I get a structure 1x1 cell without the date as row.
> str(as.numeric(returns))
num [1:1818] -0.00254 -0.00169 0.03288 0.0242 0.02781 ...
You should use the na.strings argument to read.csv (which can be passed via read.zoo), as I said in my answer to your previous question.
file <- "~/GCS/returns_Q216.csv"
returns <- read.zoo(file, FUN=as.Date, format='%d/%m/%Y', na.strings="#N/A")
returns.xts <- as.xts(returns)
I looked many entries on merging R data frames, however they are not clear to me, they talk about merging/joining using a common column, but in my case its missed or may I don't know how to extract. Here is what I am doing.
library(quantmod)
library(xts)
start = '2001-01-01'
end = '2015-08-14'
ticker = 'AAPL'
f = getSymbols(ticker, src = 'yahoo', from = start, to = end, auto.assign=F)
rsi14 <- RSI(f$AAPL.Adjusted,14)
The output I am expecting is all the columns of f and rsi14 match by date, however 'date' is not available as column, so not sure how do I join. I have to join few Moving Average columns as well.
The premise of your question is wrong. getSymbols returns an xts object, not a data.frame:
R> library(quantmod)
R> f <- getSymbols("AAPL", auto.assign=FALSE)
R> str(f)
An ‘xts’ object on 2007-01-03/2015-08-14 containing:
Data: num [1:2170, 1:6] 86.3 84 85.8 86 86.5 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "yahoo"
$ updated: POSIXct[1:1], format: "2015-08-15 00:46:49"
xts objects do not have a "Date" column. They have an index attribute that holds the datetime. xts extends zoo, so please see the zoo vignettes as well as the xts vignette and FAQ for information about how to use the classes.
Merging xts objects is as simple as:
R> f <- merge(f, rsi14=RSI(Ad(f), 14))
Or you could just use $<- to add/merge a column to an existing xts object:
R> f$rsi14 <- RSI(Ad(f), 14)
I get monthly price value for the two assets below from Yahoo:
if(!require("tseries") | !require(its) ) { install.packages(c("tseries", 'its')); require("tseries"); require(its) }
startDate <- as.Date("2000-01-01", format="%Y-%m-%d")
MSFT.prices = get.hist.quote(instrument="msft", start= startDate,
quote="AdjClose", provider="yahoo", origin="1970-01-01",
compression="m", retclass="its")
SP500.prices = get.hist.quote(instrument="^gspc", start=startDate,
quote="AdjClose", provider="yahoo", origin="1970-01-01",
compression="m", retclass="its")
I want to put these two into a single data frame with specified columnames (Pandas allows this now - a bit ironic since they take the data.frame concept from R). As below, I assign the two time series with names:
MSFTSP500.prices <- data.frame(msft = MSFT.prices, sp500= SP500.prices )
However, this does not preserve the column names [msft, snp500] I have appointed. I need to define column names in a separate line of code:
colnames(MSFTSP500.prices) <- c("msft", "sp500")
I tried to put colnames and col.names inside the data.frame() call but it doesn't work. How can I define column names while creating the data frame?
I found ?data.frame very unhelpful...
The code fails with an error message indicating no availability of as.its. So I added the missing code (which appears to have been successful after two failed attempts.) Once you issue the missing require() call you can use str to see what sort of object get.hist.quote actually returns. It is neither a dataframe nor a zoo object, although it resembles a zoo-object in many ways:
> str(SP500.prices)
Formal class 'its' [package "its"] with 2 slots
..# .Data: num [1:180, 1] 1394 1366 1499 1452 1421 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:180] "2000-01-02" "2000-01-31" "2000-02-29" "2000-04-02" ...
.. .. ..$ : chr "AdjClose"
..# dates: POSIXct[1:180], format: "2000-01-02 16:00:00" "2000-01-31 16:00:00" ...
If you run cbind on those two objects you get a regular matrix with dimnames:
> str(cbind(SP500.prices, MSFT.prices) )
num [1:180, 1:2] 1394 1366 1499 1452 1421 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:180] "2000-01-02" "2000-01-31" "2000-02-29" "2000-04-02" ...
..$ : chr [1:2] "AdjClose" "AdjClose"
You will still need to change the column names since there does not seem to be a cbind.its that lets you assign column-names. I would caution about using the data.frame method, since the object is might get confusing in its behavior:
> str( MSFTSP500.prices )
'data.frame': 180 obs. of 2 variables:
$ AdjClose :Formal class 'AsIs', 'its' [package ""] with 1 slot
.. ..# .S3Class: chr "AsIs" "its"
$ AdjClose.1:Formal class 'AsIs', 'its' [package ""] with 1 slot
.. ..# .S3Class: chr "AsIs" "its"
The columns are still S4 objects. I suppose that might be useful if you were going to pass them to other its-methods but could be confusing otherwise. This might be what you were shooting for:
> MSFTSP500.prices <- data.frame(msft = as.vector(MSFT.prices),
sp500= as.vector(SP500.prices) ,
row.names= as.character(MSFT.prices#dates) )
> str( MSFTSP500.prices )
'data.frame': 180 obs. of 2 variables:
$ msft : num 35.1 32 38.1 25 22.4 ...
$ sp500: num 1394 1366 1499 1452 1421 ...
> head(rownames(MSFTSP500.prices))
[1] "2000-01-02 16:00:00" "2000-01-31 16:00:00" "2000-02-29 16:00:00"
[4] "2000-04-02 17:00:00" "2000-04-30 17:00:00" "2000-05-31 17:00:00"
MSFT.prices is a zoo object, which seems to be a data-frame-alike, with its own column name which gets transferred to the object. Confer
tmp <- data.frame(a=1:10)
b <- data.frame(lost=tmp)
which loses the second column name.
If you do
MSFTSP500.prices <- data.frame(msft = as.vector(MSFT.prices),
sp500=as.vector(SP500.prices))
then you will get the colnames you want (though you won't get zoo-specific behaviours). Not sure why you object to renaming columns in a second command, though.