I have a large matrix (defined as a data frame). I want to multiply each value of the variables (here:
V10) rowwise with a weight (here:
Here are some sample data:
library(tidyverse) mx = matrix(data = c("abc", 0,1,2,3,4,5,6,7,8,9), nrow = 11, ncol = 11) # create some matrix key = c(NA, 1,1,1,0.5,1,0.3,0.7,1,1,0.5) # define the weights test = as.data.frame(cbind(mx, key)) # put the matrix and the weights together
In my real data, I would have some text above and on the left side of my data (because there are several col-/rownames - I didn't do the data).
The main problem is, that I have mixed data types, and that everything is formated as charachter. That's why my code to calculate the new values doesn't work:
test = test$key * test[,1:10] # or just test = test$key * test
Is there any trick how to deal with different data types? I tried to select only the parts of the columns which should be numeric, but they remain formated as character:
test[2:11,] = sapply(test[2:11,], as.numeric) # or with: test[2:11,] = numeric()
I hope, I was clear enough with describing my problem.