unable to get the highest price using runMax - r

The runMax does not work . It shows the column High not there
library(quantmod)
data <- getSymbols("GOOG",auto.assign=TRUE)
runMax(Hi(data), 30)
Error in Hi(data): subscript out of bounds: no column name containing "High"
Traceback:
1. runMax(Hi(data), 30)
2. try.xts(x, error = as.matrix)
3. is.xts(x)
4. Hi(data)
5. stop("subscript out of bounds: no column name containing \"High\"")
the error shows "High" not there

The problem is not "runMax" but auto.assign. Here is the suggestion. Using auto.assign=TRUE, the variable chosen is an R-legal name derived from the symbol being loaded (see https://www.rdocumentation.org/packages/quantmod/versions/0.4.20/topics/getSymbols)
library(quantmod)
data <- getSymbols("GOOG",auto.assign=FALSE)
runMax(Hi(data), 30)
or
library(quantmod)
getSymbols("GOOG",auto.assign=TRUE)
runMax(Hi(GOOG), 30)

Related

Error: incorrect number of dimensions & How to make an object that is the sum of the 5th column of the matrix contained in the object "myList"

I am new to coding and I am having problems with the hw question (part 1.3): "make an object called 'ans2' that is the sum of the 5th column of the matrix contained in "myList"", because I keep getting different variations of errors whenever i try to run it
The question also specifically states: *Use [], [[]], [,] notation to answer this part
first for context this is my work for the previous parts of this problem which all ran without error:
#1.1
myList <- c(1:10)
matrix(c(1:25), byrow = TRUE, nrow=5, )
list( str = "latte", vec = "taco", "nan", vec = c(1:7)) #question was Make an object called `myList` that contains the following elements (in order):The integers 1 through 10
* A matrix with the integers 1 through 25 that has five rows, filled by row
* A list that contains
- The text "latte"
- A vector with the text "taco" and "nan"
- A vector with the integers 1 through 7
#1.2
ans1 <- myList[4] #this question was: Make an object called `ans1` that is the fourth row of the matrix contained in `myList`
I tried doing this for this question:
ans2 <- colSums(myList[,c(5)], na.rm=TRUE )
But I got the error:
Error in myList[ , c(5)] : incorrect number of dimensions
Then I looked on some of the exchanges on this site and tried,
ans2 <- myList[1:nrow(myList),5]
But I got another new error:
Error in 1:nrow(myList) : argument of length 0
Would really appreciate help on how to solve this problem!

How do I get around the error "Error in WMA(x$Raw_HMA, n = sqrt(50)) : Length of 'wts' must equal the length of 'x' or 'n'"?

I am trying to calculate the HMA for a technical indicator project, and I've gotten to the last calculation I need. When I run the code, however, I am presented with an error every time stating that the length of 'wts' must equal the length of 'x' or 'n'. I performed the previous calculations without a problem so I am confused as to why this one line of code will not run.
Here is my code:
x <- Cl(stock_data) #create a container that contains the initial data, intermediate calculations, and the final technical indicator for convenience. x will eventually become the input argument to the function implementation of the technical indicator
names(x) <- "close" #rename the data column to make the code general so any stock's data can be inputted easily
x$WMA_1 <- WMA(x$close, n = 25) #create the WMA(n/2)
x$WMA_2 <- WMA(x$close, n = 50) #create the WMA(n)
x$Raw_HMA <- (2*x$WMA_1) - x$WMA_2 #create the raw HMA used to calculate the final HMA
x$HMA <- WMA(x$Raw_HMA, n = sqrt(50))
Error in WMA(x$Raw_HMA, n = sqrt(50)) : Length of 'wts' must equal the
length of 'x' or 'n'
I've tried changing x$Raw_HMA to a vector and numeric value, and I also tried specifying wts to equal the same as n, but no matter what I try I end up with the same thing.
Specifying an integer value for the n parameter to WMA will resolve your issue:
x$HMA <- WMA(x$Raw_HMA, n = floor(sqrt(50)))

Why does CrossTable fail with "'x' must have at least 2 elements" on a vector with a single value?

library(gmodels)
a <- c(4,4,4,4,4)
CrossTable(a, chisq=FALSE, prop.chisq=FALSE)
produces: Error in chisq.test(t, correct = FALSE) :
'x' must at least have 2 elements
a <- c(4,5,6,7,8)
CrossTable(a, chisq=FALSE, prop.chisq=FALSE)
produces the frequency table you'd expect.
The result is a single cell: a count of 5 of the value 4. CrossTable() wants at least 2 cells in the table.

object not find when aggregating in R

I am trying to use aggregate in R. I found an example code:
attach(mtcars)
agg=aggregate(mtcars, by=list(cyl,vs),FUN=mean, na.rm=TRUE)
detach(mtcars)
This works fine. However when I try to do it using my data:
library(stats)
FileName="Raw.csv"
Raw=read.csv(FileName,header = TRUE)
Acc1=aggregate(Raw,by=list(Experiment,SsNum),FUN=mean, na.rm=TRUE)
I get the following error message:
Error in aggregate.data.frame(Raw, by = list(Experiment, SsNum), FUN = mean, object 'Experiment' not found
I also tries to run:
Acc2=aggregate(Raw,by=list(Raw$Experiment,Raw$SsNum),FUN=mean, na.rm=TRUE)
and I got the following error:
There were 50 or more warnings (use warnings() to see the first 50)
The warnings are:
1: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
my main question is how the Acc1 is differrent from the online example (that works fine).
thank you very much
Ariel
you can just compute the mean of a numeric variable so you have at least to take a subset of the data excluding character variables. Thats were you ACC1 most likely differ from mtcars, because in mtcars there are only numeric values in, due to this you do not get a warning in the first line.
So in this line:
Acc2=aggregate(Raw,by=list(Raw$Experiment,Raw$SsNum),FUN=mean, na.rm=TRUE)
You get a error, because in RAW there appears to be column which are not numeric
Supposed you have:
set.seed(4)
Experiment <- sample(seq(1:3), 5, replace=TRUE)
SsNum <- sample(1:10, 5, replace=TRUE)
value <- rnorm(5)
df <- data.frame(Experiment, SsNum, value)
Then aggregate works as follows:
aggregate(value ~Experiment + SsNum, data = df, FUN = mean)
Experiment SsNum value
1 3 1 1.7768632
2 2 3 0.6892754
3 1 8 -1.2812466
4 1 10 0.8416977

Error in R "undefined columns selected"

I am trying to initiate this code using the zoo command:
gld <- zoo(gld[,7], gld_dates)
Unfortunately I get an error message telling me this:
Error in `[.data.frame`(gld, , 7) : undefined columns selected
I want to use the zoo function to create zoo objects from my data.
The function should take two arguments: a vector of data and
a vector of dates.
This is the data I am using[LINK BROKEN].
I believe I have have 7 columns in my data set. Any ideas?
The code I am trying to implement is found here[LINK BROKEN].
Is their anything wrong with this code?
You don't say what your gld_dates is exactly, but if gld starts as your original data and you want to make a zoo object of the 7th column ordering by the 1st column (dates), I can do
gld_zoo <- zoo(gld[, 7], gld[, 1])
just fine. Equivalently, but with more readability,
gld_zoo <- zoo(gld$Adj.close, gld$Date)
reminds me what each column is.
Subsetting requires the names of the subset columns to match those in the data frame. This code subsets the dataset french_fries with potat instead of potato:
data("french_fries")
df_potato <- french_fries[, c("potatoes")]
and it fails with:
Error in `[.data.frame`(french_fries, , c("potatoes")) :
undefined columns selected
but using the right name potato works:
df_potato <- french_fries[, c("potato")]

Resources