The code below produces the following error:
Error in 2:n : NA/NaN argument
How can I resolve this error?
library (pdfetch)
library(tidyverse)
library(xts)
tickers<-c("AXP","MMM","BA","CAT","CVX","CSCO","KO","DWDP","AAPL","XOM","GE","GS","HD","IBM","INTC","HPI","AIV","MCD","MRK","MSFT","NKE","PFE","PG","TRV","JPM","UTX","VZ","V","WMT","DIS")
data<-pdfetch_YAHOO(tickers<- c("^DJI","AXP","MMM","BA","CAT","CVX","CSCO","KO","DWDP","AAPL","XOM","GE","GS","HD","IBM","INTC","HPI","AIV","MCD","MRK","MSFT","NKE","PFE","PG","TRV","JPM","UTX","VZ","V","WMT","DIS"),from = as.Date("2015-03-20"),to = as.Date("2018-03-20"),interval='1mo')
# to remove the nas from the entire data
data[complete.cases(data),]
plus<-data[complete.cases(data),]
plus
str(plus)
head(plus)
tail(plus)
class(plus$Date)
(plus[1:10, "^DJI.adjclose",drop=F])
#Create a new data frame that contains the price data with the dates as the row names
prices <- (plus)[, "^DJI.adjclose", drop = FALSE]
rownames(prices) <-plus$Date
head(prices)
tail(prices)
#to find the return from 3/3/2015-3/8/2018
djia_ret1<- ((prices [2:n,1]-prices [1:(n-1),1])/prices [1:(n-1),1])
Error in 2:n : NA/NaN argument.
This means that one (or both) of the two arguments of : are NA or NaN. 2 is not, so n must be.
In your question you don't show how you created the variable n, but if it was the result of some data that was NA, or a division by zero result for example, that would cause these errors.
I have a matrix of 48 variables with 40 observation each. I am trying to correlate the first 47 columns separately to the 48th column. what I've tried to do is use cor command:
cor(x[,1:47], x[,48], method="kendall").
I'm getting an error:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Since each column is the same length, I understand it's not about the column lengths, but something else. what can it be?
Thanks!
David.
Look at your error message:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Hj1[48].
That was a typo. You wanted Hj1[,48]
Ok, I've separated the column from the matrix and run the correlation successfully.
In my original code it looks like this:
Hj1tox <- Hj1[,48]
Hj1_ab <- Hj1[,1:47]
cor(Hj1_ab, Hj1tox)
I have a large, sparse binary matrix in R that I want to coerce to an itemMatrix so I can perform collaborative filtering. When I run:
i <- as(m, "itemMatrix")
I get the following error:
Error in t(as(from, "ngCMatrix"))
error in evaluating the argument 'x' in selecting a method for function 't': Error in asMethod(object) : cannot coerce 'NA's to "nsparseMatrix"
When I convert the original matrix to a data frame and replace the matrix in the code, I get the following error:
Error in asMethod(object)
can not coerce list with transactions with duplicated items
I double checked my dataset for missing values and find none. What could the problem be that is preventing the conversion to an itemMatrix?
While ordering a dataframe by a specific column, I found this code to work:
sortState <- byState[order(na.omit(byState$"Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure")),]
but when I wanted to use a variable for sake of a more generalized function...
outcome <- "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure"
sortState <- byState[order(na.omit(byState$outcome)),]
I received this error message:
Error in order(na.omit(byState$outcome)) : argument 1 is not a vector
In addition: Warning message: In is.na(object) : is.na() applied to
non-(list or vector) of type 'NULL'
If you want to use a column name stored in a variable, you have to use [[ or [ instead of $:
byState[order(na.omit(byState[[outcome]])),]
I'm trying to complete a mann-whitney-wilcoxon test in R to compare brood sizes between 2 years. My data read in successfully in 2 columns, labeled x and y for each year, ranked, with unequal sample sizes. I'm getting the following error and I'm not sure what the problem is.
setwd('c:/OSPR NEST 2011 & 2012')
penob1112<-read.csv('compare_penob_11_12.csv',header=TRUE)
wilcox.test(x, y, data=penob1112)
Error in wilcox.test(x, y, data = penob1112) : object 'x' not found
Thanks for any insights!
The data argument is only taken when the first argument is of class formula. You need to explicitly call each object instead:
wilcox.test(penob1112$x, penob1112$y)
Look at ?wilcox.test - it has two methods (default and formula)