Transportation work dataframe.
c1 <- c(1,2,3) c2 <- c(4,5,6) c3 <- c(7,8,9) d1 <- data.frame(c1,c2,c3) v1 <- c(1,2,3) t(t(d1)*v1) # c1 c2 c3 #[1,] 1 8 21 #[2,] 2 10 24 #[3,] 3 12 27
EDIT: if all columns are not numeric, you can do the following
c1 <- c(1,2,3) c2 <- c(4,5,6) c3 <- c(7,8,9) d1 <- data.frame(c1,c2,c3) # Adding a column of characters for demonstration d1$c4 <- c("rr", "t", "s") v1 <- c(1,2,3) #Choosing only numeric columns index <- which(sapply(d1, is.numeric) == TRUE) d1_mat <- as.matrix(d1[,index]) d1[,index] <- t(t(d1_mat)*v1) d1 # c1 c2 c3 c4 #1 1 8 21 rr #2 2 10 24 t #3 3 12 27 s
Greenparker
source share