我正在使用R包mRMRe进行特征选择,并尝试从整体结果中获取最常见特征的索引:
ensemble <- mRMR.ensemble(data = dd, target_indices = target_idx,solution_count = 5, feature_count = 30)
features_indices = as.data.frame(solutions(ensemble))
这给我以下数据:
MR_1 MR_2 MR_3 MR_4 MR_5
2793 2794 2796 2795 2918
1406 1406 1406 1406 1406
2798 2800 2798 2798 2907
2907 2907 2907 2907 2800
2709 2709 2709 2709 2709
1350 2781 1582 1350 1582
2781 1350 2781 2781 636
2712 2712 2712 2712 2781
636 636 636 636 2779
2067 2067 2067 2067 2712
2328 2328 2357 2357 2067
2357 783 2328 2328 2328
772 2357 772 772 772
我想使用某种投票逻辑为所有列中的每一行选择最常用的索引。
例如,在上图中:
1. For the first row there is no match - so select the first one.
2. There are some rows where min occurrence is 2 - so select that one.
3. In case of tie - check if any occurs thrice, if yes select that one, or else from the tied indices select the first occurring one.
可能是我太复杂了,但基本上我想从数据框中的每一行的所有索引中选择最佳索引。
有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
这是使用apply
:
apply(df, 1, function(x) { names(which.max(table(x))) })
给出:
[1] "2793" "1406" "2798" "2907" "2709" "1350" "2781" "2712" "636" "2067" "2328" "2328" "772"
对于每一行,函数table
计算每个唯一元素的出现次数,然后我们返回具有最大出现次数的元素的名称(如果存在平局,则选择第一个)。 / p>