3

我一直在为此挠头。我有两个数据框: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)

我只想将这两个数据框乘以相同的列名。我试过这样的事情:

colwise(function(x) {x * weights[names(x)]})(df)

但这显然不起作用,因为colwise没有将列名保留在函数内。我查看了各种mapply解决方案(示例),但无法找到答案。

结果data.frame应如下所示:

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
4

5 回答 5

6

sweep()是你的朋友,对于这个特殊的例子。它依赖于名称中的名称dfweights正确的顺序,但可以安排。

> nams <- names(weights)
> df[, nams] <- sweep(df[, nams], 2, unlist(weights), "*")
> df
  group age income assets
1     1 150   1000    500
2     2 200   3000    650
3     3 250   5000    800

weights如果和中的变量名df顺序不同,可以这样:

> df2 <- 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))
> nams <- c("age", "income") ## order in df2
> weights2 <- weights[, rev(nams)]
> weights2  ## wrong order compared to df2
  income age
1     10   5
> df2[, nams] <- sweep(df2[, nams], 2, unlist(weights2[, nams]), "*")
> df2
  group age income assets
1     1 150   1000    500
2     2 200   3000    650
3     3 250   5000    800

换句话说,我们对所有对象重新排序,以便ageincome处于正确的顺序。

于 2012-10-22T20:43:00.423 回答
4

有人可能有一种巧妙的方法来使用 plyr,但这可能是基础 R 中最直接的方法。

shared.names <- intersect(names(df), names(weights))
cols <- sapply(names(df), USE.NAMES=TRUE, simplify=FALSE, FUN=function(name) 
        if (name %in% shared.names) df[[name]] * weights[[name]] else df[[name]])
data.frame(do.call(cbind, cols))

#   group age income assets
# 1     1 150   1000    500
# 2     2 200   3000    650
# 3     3 250   5000    800
于 2012-10-22T20:32:24.360 回答
3

您的数据:

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 <- data.frame(age = 5, income = 10)

逻辑:

# Basic name matching looks like this
names(df[names(df) %in% names(weights)])
# [1] "age"    "income"

# Use that in `sapply()`
sapply(names(df[names(df) %in% names(weights)]), 
       function(x) df[[x]] * weights[[x]])
#      age income
# [1,] 150   1000
# [2,] 200   3000
# [3,] 250   5000

实施:

# Put it all together, replacing the original data
df[names(df) %in% names(weights)] <- sapply(names(df[names(df) %in% names(weights)]), 
                                            function(x) df[[x]] * weights[[x]])

结果:

df
#   group age income assets
# 1     1 150   1000    500
# 2     2 200   3000    650
# 3     3 250   5000    800
于 2012-10-22T20:37:18.123 回答
2

这是一个 data.table解决方案

library(data.table)
DT <- data.table(df)
W <- data.table(weights)

使用mapply(或Map)计算新列,然后通过引用一次添加两者。

DT <- data.table(df)
W <- data.table(weights)


DT[, `:=`(names(W), Map('*', DT[,names(W), with = F], W)), with = F]
于 2012-10-22T22:00:11.377 回答
0

您也可以在 for 循环中使用由 which(%in%) 生成的索引来执行此操作。上述方法效率更高,但这是一种替代方法。

results <- list()
  for ( i in 1:length(which(names(df) %in% names(weights))) ) {
    idx1 <- which(names(df) %in% names(weights))[i]
      idx2 <- which(names(weights) %in% names(df))[i] 
    results[[i]] <- dat[,idx1] * weights[idx2] 
  } 
unlist(results)  
于 2012-10-22T20:41:05.837 回答