如何在 r 中计算要减去的两个数据帧列的平均值 "mean(df1$a-df2$b)"

how to do the mean of two dataframes columns to be subtrated "mean(df1$a-df2$b)" in r

我的两个数据框如下所示:

> dput(head(df1,25))
structure(list(Date = structure(c(16644, 16645, 16646, 16647, 
16648, 16649, 16650, 16651, 16652, 16653, 16654, 16655, 16656, 
16657, 16658, 16659, 16660, 16661, 16662, 16663, 16664, 16665, 
16666, 16667, 16668), class = "Date"), AU = c(0.241392906920806, 
0.257591745069017, 0.263305712230276, NaN, 0.252892547032525, 
0.251771180928526, 0.249211746794207, 0.257289083109259, 0.205017582640463, 
0.20072274573488, 0.210154167590338, 0.207384553271337, 0.193725450540089, 
0.199282601988984, 0.216267134143314, 0.217052471451736, NaN, 
0.220703029531909, 0.2164619798534, 0.223442036108148, 0.22061326758891, 
NaN, 0.277777461504811, NaN, 0.200839628485262)), row.names = c(NA, 
-25L), class = c("tbl_df", "tbl", "data.frame"))

> dput(head(df2,25))
structure(list(UF1 = c(0.2559, 0.2565, 0.257, 0.2577, 0.2583, 
0.259, 0.2596, 0.2603, 0.2611, 0.2618, 0.2625, 0.2633, 0.2641, 
0.2649, 0.2657, 0.2665, 0.2674, 0.2682, 0.2691, 0.27, 0.2709, 
0.2718, 0.2727, 0.2736, 0.2745), UF2 = c(0.2597, 0.2602, 0.2608, 
0.2614, 0.2621, 0.2627, 0.2634, 0.2641, 0.2648, 0.2655, 0.2663, 
0.267, 0.2678, 0.2686, 0.2694, 0.2702, 0.2711, 0.2719, 0.2728, 
0.2737, 0.2745, 0.2754, 0.2763, 0.2773, 0.2782), UF3 = c(0.2912, 
0.2915, 0.2918, 0.2922, 0.2926, 0.293, 0.2934, 0.2938, 0.2943, 
0.2947, 0.2952, 0.2957, 0.2962, 0.2968, 0.2973, 0.2979, 0.2985, 
0.2991, 0.2997, 0.3003, 0.3009, 0.3016, 0.3022, 0.3029, 0.3035
), Date = structure(c(16644, 16645, 16646, 16647, 16648, 16649, 
16650, 16651, 16652, 16653, 16654, 16655, 16656, 16657, 16658, 
16659, 16660, 16661, 16662, 16663, 16664, 16665, 16666, 16667, 
16668), class = "Date")), row.names = c(NA, 25L), class = "data.frame")
>

我想计算两个不同数据帧列的平均值相减 (mean(df1$AU-df2$UF))。 最接近我得到的解决方案如下:

data.frame(mean = colMeans(df1$AU, na.rm = TRUE) - colMeans(df2$UF))

但是我得到了这个错误:

Error in colMeans(df1$mAU, na.rm = TRUE) : 
  'x' must be an array of at least two dimensions

我成功地 运行 相同的代码仅适用于每个数据帧只有一列,但由于每个数据帧有 3 列或更多列,我想针对 df1$AU 进行计算,我需要提高效率。

任何帮助将不胜感激。谢谢。

假设你的意思是你想用 df2 中的(数字)列的平均值减去 df1 中的(数字)列的平均值,这是可以完成的像这样:

mean(df1$AU, na.rm = T) - colMeans(df2[,1:3], na.rm = T)

这输出:

       UF1        UF2        UF3 
-0.0367389 -0.0404509 -0.0688949

df2

的每列

希望对您有所帮助。

这里有两个基本的 R 函数来计算差异的平均值。第二个更快。

meanDiffs1 <- function(x, y, na.rm = TRUE){
  z <- if(na.rm) na.omit(cbind(x, -1*y)) else cbind(x, -1*y)
  mean(rowSums(z))
}
meanDiffs2 <- function(x, y, na.rm = TRUE){
  if(na.rm){
    i <- is.na(x)
    j <- is.na(y)
    mean(x[!i & !j] - y[!i & !j])
  } else {
    mean(x - y)
  }
}

meanDiffs(df1$AU, df2$UF1)
#[1] -0.0361429
meanDiffs2(df1$AU, df2$UF1)
#[1] -0.0361429

要计算 df1$AUdf$UF* 之间的所有平均差,请使用 sapply

sapply(df2[1:3], \(y) meanDiffs2(df1$AU, y))
#        UF1         UF2         UF3 
#-0.03614290 -0.03986195 -0.06848576