r - I want to create a vector by using values of variables from a dataframe -
data <- data.frame(cust_id = 1:5, a= (10,20,30,40,50), b=c(5,10,15,20,25), c=c(20,40,60,80,100), d=c(2,4,6,8,10)) vect <- c(a*2,b*3,c*4,d*5)
here need call values of a,b,c,d dataset respect cust_id 1:5 vector , multiply them 2,3,4,5 constants
vect<- c(40*2,20*3,80*4,8*5)
*note: vect vector, need output in vector form,
a,b,c,d values of variables data respect cust_id
we can use crossprod
(if need multiply , sum
)
crossprod(t(data[-1]), 2:5)
if need multiply corresponding columns vector
sweep(data[-1], 2, 2:5, "*")
or mapply
mapply(`*`, data[-1], 2:5)
Comments
Post a Comment