Reputation: 169
Suppose we have the following data-set:
dat<-data.frame(num = 20:29, names = c(rep("Harry",2), rep("Gary",2), rep("Dairy",3), rep("Harry", 3)))
num names
1 20 Harry
2 21 Harry
3 22 Gary
4 23 Gary
5 24 Dairy
6 25 Dairy
7 26 Dairy
8 27 Harry
9 28 Harry
10 29 Harry
And we also have the following values for each factor level:
fvals <- c(Harry = 1, Gary = 2, Dairy = 3)
The goal is to multiply the num
level by these factors by the fvals
according to the names
level (matching it in the fvals
variable). For example, the desired output for this set of data should be
20 # 20 * 1
21 # 21 * 1
44 # 22 * 2
46 # 23 * 2
72 # 24 * 3
75 # 25 * 3
78 # 26 * 3
27 # 27 * 1
28 # 28 * 1
29 # 29 * 1
I have been doing this by converting the factor variable into a matrix with binary variables for each level, and then proceeding with matrix multiplication. But it was quite confusing trying to convert the matrices / vectors such that R can do multiplication on (and such that the level columns match). Also I'm not sure if the mat mul method will be efficient with a large number of observations. Just wondering if there's a better alternative to do this.
Upvotes: 0
Views: 949
Reputation: 39154
Here is an idea. Notice that I set stringsAsFactors = FALSE
because it is easier to work with character vector directly.
dat<-data.frame(num = 20:29,
names = c(rep("Harry",2), rep("Gary",2), rep("Dairy",3), rep("Harry", 3)),
stringsAsFactors = FALSE)
fvals <- c(Harry = 1, Gary = 2, Dairy = 3)
dat$num * fvals[dat$names]
# Harry Harry Gary Gary Dairy Dairy Dairy Harry Harry Harry
# 20 21 44 46 72 75 78 27 28 29
Upvotes: 2