R 从 table 矩阵中提取变量映射

R extracting variable mapping from table matrix

这里有点脑筋急转弯 - 我可以手动完成,但必须有线性代数方法才能完成。

我使用 table 函数得出了一个 dataframe/matrix,其中包含两个变量的重合计数:

>table(gfk_data$AnswerID, gfk_data$AnswerText)


          Very unlikely Unlikely Somewhat likely Very likely
  1161841             0        0               0        3029
  1161842             0        0             175           0
  1161843             0      165               0           0
  1161844           562        0               0           0
  1161845             0        0               0          31
  1161846             0        0              26           0
  1161847             0       26               0           0
  1161848            45        0               0           0

如何获取行名 AnswerID 到 headers 列 AnswerText 的映射?

也许这更容易从原始数据框中提取出来?

期望的结果类似于:

AnswerID  AnswerText   
1161841   Very likely
1161842   Somewhat likely
1161843   Unlikely
...

到目前为止,我可以想到使用循环来查找原始数据框中每个 AnswerID 的 AnswerText 值,并将 return 的 unique() 插入到数据框中。那肯定行得通,但也许还有更优雅的东西?

这是从上面的输出中获取它的一种方法。 "trick" 构造第一列是使用查找 table ,通过每列的最大值提取正确的行名称。

dfNew <- data.frame(answerID=rownames(df)[max.col(df)],
                    answerText=names(df), stringsAsFactors=F)

  answerID      answerText
1 1161844   Very_unlikely
2 1161843        Unlikely
3 1161842 Somewhat_likely
4 1161841     Very_likely
5 1161844   Very_unlikely
6 1161843        Unlikely
7 1161842 Somewhat_likely
8 1161841     Very_likely

。 data.frame 可以使用 order

重新排序
dfNew <- dfNew[order(dfNew$answerID),]
dfNew
  answerID      answerText
4  1161841     Very_likely
8  1161841     Very_likely
3  1161842 Somewhat_likely
7  1161842 Somewhat_likely
2  1161843        Unlikely
6  1161843        Unlikely
1  1161844   Very_unlikely
5  1161844   Very_unlikely

数据

df <- read.table(header=T, text="         Very_unlikely Unlikely Somewhat_likely Very_likely
1161841             0        0               0        3029
1161842             0        0             175           0
1161843             0      165               0           0
1161844           562        0               0           0
1161845             0        0               0          31
1161846             0        0              26           0
1161847             0       26               0           0
1161848            45        0               0           0")

您可以使用 as.data.frame.table() 并提取第三列大于 0 的前两列:

as.data.frame.table(
    table(gfk_data$AnswerID, gfk_data$AnswerText)
    )[as.data.frame.table(table(gfk_data$AnswerID, gfk_data$AnswerText))[,3] > 0, c(1,2)]

我发现我可以将生成的矩阵放入数据框中,然后根据大于 0 的项目频率进行过滤。

count_df <- as.data.frame(table(gfk_data$AnswerID, gfk_data$AnswerText))
count_df[count_df$Freq>1,]

那个数据框的前两列会给我我想要的结果

      Var1            Var2 Freq
4  1161844   Very unlikely  562
8  1161848   Very unlikely   45
11 1161843        Unlikely  165
15 1161847        Unlikely   26
18 1161842 Somewhat likely  175
22 1161846 Somewhat likely   26
25 1161841     Very likely 3029
29 1161845     Very likely   31

另一个使用@lmo df 的选项是

data.frame(answerID=rownames(df),answerText=names(df)[apply(df,1,which.max)])