计算R中具有特定名称模式的列的行含义

问题描述 投票:2回答:1

我的数据集看起来像这样:

> head(big_metric[,c(30:37)])
   Recall_41365 Accuracy_41361 Precision_41361 Recall_41361 Accuracy_41365 Precision_41365 Recall_41365 Prob
1:    1.0000000      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.0
2:    1.0000000      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.1
3:    1.0000000      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.2
4:    1.0000000      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.3
5:    1.0000000      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.4
6:    0.9936909      0.9516129       0.9516129            1      0.8189655       0.8189655            1  0.5

数据集是10行,列big_metric$Prob具有从00.9的值,具有0.1步骤。

对于每一行,我想计算除mean之外的所有列的Prob值。 mean值将存储在新列中,如下所示:

qazxsw poi:qazxsw poi值为具有标题模式qazxsw poi的列

qazxsw poi:qazxsw poi值为具有标题模式qazxsw poi的列

qazxsw poi:qazxsw poi值为具有标题模式qazxsw poi的列

我对R很新,所以如果至少有人能让我走上正轨,那将是一个重要的帮助。任何输入都表示赞赏!

以下是数据样本:

big_metric$Accuracy
r mean
1个回答
2
投票

基于mean,更容易的选择是Accuracy_413**(或者可能需要与big_metric$Precision做一些重塑

mean

或者我们将Precision_413**变成'long'格式然后获得列的big_metric$Recall

mean

Update

如果我们还需要行标准偏差,请使用Recall_413**中的> dput(big_metric) structure(list(Accuracy_41365 = c(0.89119171, 0.89119171, 0.89119171, 0.89119171, 0.89119171, 0.888601036, 0.886010363, 0.883419689, 0.867875648, 0.870466321), Precision_41365 = c(0.89119171, 0.89119171, 0.89119171, 0.89119171, 0.89119171, 0.890909091, 0.901069519, 0.909589041, 0.910364146, 0.92), Recall_41365 = c(1, 1, 1, 1, 1, 0.997093023, 0.979651163, 0.965116279, 0.944767442, 0.936046512 ), Accuracy_41366 = c(0.836898396, 0.836898396, 0.836898396, 0.836898396, 0.836898396, 0.820855615, 0.839572193, 0.839572193, 0.831550802, 0.826203209), Precision_41366 = c(0.836898396, 0.836898396, 0.836898396, 0.836898396, 0.836898396, 0.83423913, 0.856338028, 0.870967742, 0.87202381, 0.892405063), Recall_41366 = c(1, 1, 1, 1, 1, 0.980830671, 0.971246006, 0.948881789, 0.936102236, 0.900958466), Accuracy_41365 = c(0.810344828, 0.810344828, 0.810344828, 0.810344828, 0.810344828, 0.793103448, 0.801724138, 0.818965517, 0.801724138, 0.793103448), Precision_41365 = c(0.810344828, 0.810344828, 0.810344828, 0.810344828, 0.810344828, 0.807017544, 0.825688073, 0.841121495, 0.838095238, 0.836538462), Recall_41365 = c(1, 1, 1, 1, 1, 0.978723404, 0.957446809, 0.957446809, 0.936170213, 0.925531915), Accuracy_41366 = c(0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.926605505, 0.889908257, 0.844036697, 0.779816514), Precision_41366 = c(0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.972477064, 0.99, 1, 1, 1), Recall_41366 = c(1, 1, 1, 1, 1, 1, 0.933962264, 0.886792453, 0.839622642, 0.773584906), Accuracy_41361 = c(0.851162791, 0.851162791, 0.851162791, 0.851162791, 0.851162791, 0.837209302, 0.809302326, 0.795348837, 0.776744186, 0.795348837), Precision_41361 = c(0.851162791, 0.851162791, 0.851162791, 0.851162791, 0.851162791, 0.849056604, 0.858585859, 0.863874346, 0.860962567, 0.879781421), Recall_41361 = c(1, 1, 1, 1, 1, 0.983606557, 0.928961749, 0.901639344, 0.879781421, 0.879781421), Accuracy_41366 = c(0.703208556, 0.703208556, 0.703208556, 0.703208556, 0.703208556, 0.697860963, 0.711229947, 0.703208556, 0.684491979, 0.687165775), Precision_41366 = c(0.703208556, 0.703208556, 0.703208556, 0.703208556, 0.703208556, 0.701612903, 0.715877437, 0.717142857, 0.711370262, 0.715976331), Recall_41366 = c(1, 1, 1, 1, 1, 0.992395437, 0.977186312, 0.954372624, 0.927756654, 0.920152091), Accuracy_41361 = c(0.790322581, 0.790322581, 0.790322581, 0.790322581, 0.790322581, 0.758064516, 0.725806452, 0.661290323, 0.629032258, 0.612903226), Precision_41361 = c(0.790322581, 0.790322581, 0.790322581, 0.790322581, 0.790322581, 0.783333333, 0.785714286, 0.769230769, 0.76, 0.755102041), Recall_41361 = c(1, 1, 1, 1, 1, 0.959183673, 0.897959184, 0.816326531, 0.775510204, 0.755102041 ), Accuracy_41366 = c(0.798165138, 0.798165138, 0.798165138, 0.798165138, 0.798165138, 0.788990826, 0.76146789, 0.752293578, 0.724770642, 0.706422018), Precision_41366 = c(0.798165138, 0.798165138, 0.798165138, 0.798165138, 0.798165138, 0.796296296, 0.796116505, 0.8, 0.793814433, 0.795698925), Recall_41366 = c(1, 1, 1, 1, 1, 0.988505747, 0.942528736, 0.91954023, 0.885057471, 0.850574713 ), Accuracy_41361 = c(0.874418605, 0.874418605, 0.874418605, 0.874418605, 0.874418605, 0.860465116, 0.846511628, 0.846511628, 0.841860465, 0.841860465), Precision_41361 = c(0.874418605, 0.874418605, 0.874418605, 0.874418605, 0.874418605, 0.872641509, 0.87804878, 0.885572139, 0.885, 0.896907216), Recall_41361 = c(1, 1, 1, 1, 1, 0.984042553, 0.957446809, 0.946808511, 0.941489362, 0.925531915 ), Accuracy_41365 = c(0.821243523, 0.821243523, 0.821243523, 0.821243523, 0.821243523, 0.816062176, 0.816062176, 0.810880829, 0.803108808, 0.792746114), Precision_41365 = c(0.821243523, 0.821243523, 0.821243523, 0.821243523, 0.821243523, 0.8203125, 0.828877005, 0.82972973, 0.828337875, 0.831932773), Recall_41365 = c(1, 1, 1, 1, 1, 0.993690852, 0.977917981, 0.968454259, 0.958990536, 0.936908517), Accuracy_41361 = c(0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.919354839, 0.919354839, 0.887096774, 0.870967742), Precision_41361 = c(0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.951612903, 0.965517241, 0.965517241, 0.964285714, 0.981132075), Recall_41361 = c(1, 1, 1, 1, 1, 1, 0.949152542, 0.949152542, 0.915254237, 0.881355932 ), Accuracy_41365 = c(0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.810344828, 0.801724138, 0.801724138, 0.793103448), Precision_41365 = c(0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.818965517, 0.834862385, 0.839622642, 0.839622642, 0.844660194), Recall_41365 = c(1, 1, 1, 1, 1, 1, 0.957894737, 0.936842105, 0.936842105, 0.915789474 ), Prob = c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)), row.names = c(NA, -10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000052e1ef0>) 。和以前一样,我们dput the big_metric,循环splitmelt# find the column names that start with 'Recall', 'Accuracy', 'Precision' nm1 <- grep("^(Recall|Accuracy|Precision)", names(big_metric), value = TRUE) # split the dataset with the substring of nm1 into a list # loop through the list and get the rowMeans out <- sapply(split.default(big_metric[, ..nm1], sub("_.*", "", nm1)), rowMeans) # assign that columns to create new columns in the original dataset big_metric[, colnames(out) := as.data.frame(out)] meltmean创造out1 <- melt(big_metric[, rn := seq_len(.N)], measure = patterns("^Recall", "^Precision", "^Accuracy"))[, lapply(.SD, mean), rn, .SDcols = value1:value3] big_metric[, c("Recall", "Precision", "Accuracy") := out1[, 2:4]] rowSds,然后matrixStats split元素创造out1。将输出列指定为原始数据集,并使用准备的列名称向量

list
© www.soinside.com 2019 - 2024. All rights reserved.