Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R argument is missing, with no default

Tags:

r

csv

I want to calculate the log return of data . I define a function and want to load the data. but system always mentions second factor is missing. Otherwise it just calculate the log of row number.

#read data
data <- read.csv(file="E:/Lect-1-TradingTS.csv",header=TRUE)
mode(data)
p<-data["Price"]


#func1
func1 <- function(x1,x2)
{
  result <- log(x2)-log(x1)
  return(result)
} 


#calculate log return
log_return<-vector(mode="numeric", length=(nrow(data)-1))
for(i in 2:nrow(p))
{
  log_return[i-1] <- func1(p[(i-1):i])
}

Error in func1(p[(i - 1):i]) : argument "x2" is missing, with no default


like image 714
xin ding Avatar asked Feb 27 '26 17:02

xin ding


1 Answers

Your function func1 was defined to accept two arguments, but you are passing it a single argument: the vector p[(i-1):i], which has two elements but is still considered a single object. To fix this you need to pass two separate arguments, p[i-1] and p[i]. Alternatively, modify the definition of func1 to accept a two-element vector:

func1 <- function(v)
{
  x1 <- v[1]
  x2 <- v[2]
  result <- log(x2)-log(x1)
  return(result)
} 
like image 167
grand_chat Avatar answered Mar 02 '26 05:03

grand_chat