私はこれについて頭を悩ませてきました。2 つのデータ フレームがあります。df
df <- data.frame(group = 1:3,
age = seq(30, 50, length.out = 3),
income = seq(100, 500, length.out = 3),
assets = seq(500, 800, length.out = 3))
とweights
weights <- data.frame(age = 5, income = 10)
これら 2 つのデータ フレームを同じ列名に対してのみ乗算したいと思います。私はこのようなことを試しました:
colwise(function(x) {x * weights[names(x)]})(df)
but that obviously didn't work as colwise
does not keep the column name inside the function. I looked at various mapply
solutions (example), but I am unable to come up with an answer.
The resulting data.frame
should look like this:
structure(list(group = 1:3, age = c(150, 200, 250), income = c(1000,
3000, 5000), assets = c(500, 650, 800)), .Names = c("group",
"age", "income", "assets"), row.names = c(NA, -3L), class = "data.frame")
group age income assets
1 1 150 1000 500
2 2 200 3000 650
3 3 250 5000 800