for a given data frame I would like to multiply values of an array to a column of the data frame. The data frame consists of rows, containing a name, a numerical value and two factor values:
name credit gender group
n1 10 m A
n2 20 f B
n3 30 m A
n4 40 m B
n5 50 f C
This data frame can be generated using the commands:
name <- c('n1','n2','n3','n4','n5')
credit <- c(10,20,30,40,50)
gender <- c('m','f','m','m','f')
group <- c('A','B','A','B','C')
DF <-data.frame(cbind(name,credit,gender,group))
# binds columns together and uses it as a data frame
Additionally we have a matrix derived from the data frame (in more complex cases this will be an array). This matrix contains the sum value of all contracts that fall into a particular category (characterized by m/f and A/B/C):
m f
A 40 NA
B 40 20
C NA 50
The goal is to multiply the values in DF$credit by using the corresponding value assigned to each category in the matrix, e.g. the value 10 of the first row in DF would be multiplied by 40 (the category defined by m and A).
The result would look like:
name credit gender group result
n1 10 m A 400
n2 20 f B 400
n3 30 m A 1200
n4 40 m B 1600
n5 50 f C 2500
If possible, I would like to perform this using the R base package but I am open for any helpful solutions that work nicely.
You can construct a set of indices into derived
(being your derived matrix) by making an index matrix out of DF$group
and DF$gender
. The reason the as.character
is there is because DF$group
and DF$gender
are factors, whereas I just want character indices.
>idx = matrix( c(as.character(DF$group),as.character(DF$gender)),ncol=2)
>idx
[,1] [,2]
[1,] "A" "m"
[2,] "B" "f"
[3,] "A" "m"
[4,] "B" "m"
[5,] "C" "f"
>DF$result = DF$credit * derived[idx]
Note with that last line, using the code you have above to generate DF
, your numeric columns turn out as factors (ie DF$credit
is a factor). In that case you need to do as.numeric(DF$credit)*derived[idx]
. However, I imagine that in your actual data your data frame doesn't have DF$credit
as a factor but instead as a numeric.