Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function always returns numeric(0) [closed]

Tags:

r

I'm quite new to R, so I apologize if this is a trivially easy question. I tried Googling, but could not find examples that seemed completely relevant (most examples only have 1 function argument).

I have a simple function representing a basic linear model in 2 dimensions:

y <- function(x, w) {
  temp <- w[0] + x*w[1]
  return(temp)
}

When I use it the way I expect, I get:

> y(1,c(-0.3,0.5))
numeric(0)

When I try an incorrect input, same thing!

> y(1,2)
numeric(0)

Ultimately, my goal is to be able to have a vector, X, that can be passed in as the argument. For example:

> y(c(1,2,3,4),c(1,2))
like image 593
icz Avatar asked Feb 21 '13 17:02

icz


1 Answers

As pointed out by joran, this is because vectors in R are 1-indexed.

Thanks, joran! (And doh!)

like image 55
icz Avatar answered Oct 05 '22 22:10

icz