Reputation: 191
I have a set of vectors, l
:
l <- list(c(1,0), c(0,1))
and vector v
:
v <- c(2,2)
Say I want to find the dot product between the first vector in l and vector v, then the code should be:
l[1] %*% v
yet I get the following error:
Error in l[1] %*% v : requires numeric/complex matrix/vector arguments
if I create a new vector containing the first vector in L, the dot product executes properly. What am I doing wrong for l[1]
to not work?
l1 <- l[1]
l1 %*% v # = 2
Upvotes: 1
Views: 147
Reputation: 887671
We need to extract the list
element with [[
as l[1]
is still a list
with one element
l[[1]] %*% v
[,1]
[1,] 2
To do this on all the elements,
lapply(l, `%*%`, v)
[[1]]
[,1]
[1,] 2
[[2]]
[,1]
[1,] 2
Upvotes: 3