[R] dot products
R. Michael Weylandt <michael.weylandt@gmail.com>
michael.weylandt at gmail.com
Wed Mar 7 21:30:16 CET 2012
as.matrix(df) %*% vec
Michael
On Mar 7, 2012, at 3:14 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
> Hello,
>
> I need to take a dot product of each row of a dataframe and a vector.
> The number of columns will be dynamic. The way I've been doing it so
> far is contorted. Is there a better way?
>
> dotproduct <- function(dataf, v2) {
> apply(t(t(as.matrix(a)) * v2),1,sum) #contorted!
> }
>
> df = data.frame(a=c(1,2,3),b=c(4,5,6))
> vec = c(4,5)
> dotproduct(df, vec)
>
>
> thanks,
> allie
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list