Reputation: 1089
I'm sure this is something easy to do in R. I've attempted to search for dot products in the R documentation as well as online without success.
How can I take the dot product in R of the below matrix y and the named vector x, using only the named elements in x?
x <- c(first=1, second=2, third=3)
x2 <- c(first=1, second=2, third=3)
y <- as.matrix(rbind(x2,x2))
y
y %*% x
y %*% x [,1] x2 14 x2 14
this works
Now, I add a date field to y, and it fails:
x <- c(first=1, second=2, third=3)
x2 <- c(first=1, second=2, third=3,anchor=as.Date('2017-04-01'))
y <- as.matrix(rbind(x2,x2))
y
y %*% x
Error in x %*% y : non-conformable arguments
This is a toy example, but I might have hundreds of unfriendly date fields in the matrix, so just removing anchor
will not be an acceptable solution. Automated removal of any problem fields could be considered valid.
Upvotes: 1
Views: 2803
Reputation: 26
You must have only 3 columns (instead of 4) in the y matrix to do this operation with the 3 elements vector.
In your example, you can explicitly specify the columns you want to use, with their name:
y[,c("first","second","third")] %*% x
or with a range:
y[,1:3] %*% x
Upvotes: 1