按因子级别的顺序对数据框中行内的值进行排序?
因此,我正在尝试使用 R Shiny 创建一个用于桌面角色扮演游戏的工具,允许玩家自动生成随机幽灵。这些统计数据是因素,顺序为“最高”、“良好”、“中等”、“较差”、“糟糕”和“最差”。为了创建幻影,我需要采用这些因素的向量,长度为 25 个项目,随机化顺序,然后将其放入 5x5 数据框中,然后将两行从最好到最差进行排序。
目前,基本的 R 代码(除了闪亮的东西,因为它只是让事情变得复杂,而且不是这里的主要问题)看起来像这样:
arcanaVector <- c(rep("Supreme", 3),
rep(c("Good", "Moderate", "Poor", "Awful"), each = 5),
rep("Worst", 2))
arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
arcanaVector <- factor(arcanaVector, ordered = TRUE, levels = arcanaLevels)
shuffled_arcana <- sample(arcanaVector)
arcana_table <- as.data.frame(matrix(shuffled_arcana,
nrow = 5, ncol = 5))
row.names(arcana_table) <- c("Presence", "Manner", "Expression", "Complexity", "Tradition")
大部分代码正在工作,并且它产生的输出看起来有点像这样:
行名称 | Col1 | Col2 | Col3 | Col4 | Col5 |
---|---|---|---|---|---|
状态 | 糟糕 | 好 | 差 最高 | 最差 | 中等 |
态度 | 好 糟糕 | 中等 | 最高 | 表达 | 糟糕 |
方式 | 差 | 差 | 可怕 | 糟糕 | 复杂 |
性 | 差 | 好 | 最坏 | 好 | 好 |
传统 | 最高 | 中等 | 中等 | 中等 | 差 |
但是,无论我做什么,我似乎都无法对其进行正确排序。我尝试过 order(),我尝试过 sort(),我查看了 StackExchange 上可以看到的所有代码,但找不到任何有帮助的内容。充其量,它什么也没做;最坏的情况是,它不仅无法排序,而且还删除了因子值,以便仅显示基础数字。
例如,一旦在“存在”行上排序,它应该看起来像这样:
行名称 | Col1 | Col2 | Col3 | Col4 | Col5 |
---|---|---|---|---|---|
Presence | Supreme | Good | Poor | Awful | Worst |
Manner | Good | Awful | Moderate | Moderate | Supreme |
Expression | Awful | Poor | Poor | Awful | Awful |
Complexity | Poor | Good | Worst | Good | Good |
Tradition | Supreme | 中等 | 中等 | 中等 | 差 |
So, I'm trying to create a tool for a tabletop roleplaying game using R Shiny, allowing players to automatically generate random ghosts. These stats are factors, with the order of "Supreme", "Good", "Moderate", "Poor", "Awful", and "Worst", in order. In order to create the ghosts, I need to take a vector of these factors 25 items long, randomize the order, then put it into a 5x5 data frame before sorting two rows from best to worst.
At the moment, the basic R code (Shiny stuff aside, since it's just complicating things and it's not the primary issue here) looks like this:
arcanaVector <- c(rep("Supreme", 3),
rep(c("Good", "Moderate", "Poor", "Awful"), each = 5),
rep("Worst", 2))
arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
arcanaVector <- factor(arcanaVector, ordered = TRUE, levels = arcanaLevels)
shuffled_arcana <- sample(arcanaVector)
arcana_table <- as.data.frame(matrix(shuffled_arcana,
nrow = 5, ncol = 5))
row.names(arcana_table) <- c("Presence", "Manner", "Expression", "Complexity", "Tradition")
This much of the code is working, and it's producing an output that looks sort of like this:
Row Names | Col1 | Col2 | Col3 | Col4 | Col5 |
---|---|---|---|---|---|
Presence | Awful | Good | Poor | Supreme | Worst |
Manner | Good | Awful | Moderate | Moderate | Supreme |
Expression | Awful | Poor | Poor | Awful | Awful |
Complexity | Poor | Good | Worst | Good | Good |
Tradition | Supreme | Moderate | Moderate | Moderate | Poor |
However, no matter what I do, I can't seem to get it to sort properly. I've tried order(), I've tried sort(), I've looked over all the code I could see on StackExchange and I couldn't find anything that helped. At best, it's done nothing; at worst, it's not only failed to sort, but stripped away the factor values so that it was just displaying the underlying numbers.
Once sorted on the Presence row, for instance, it should look something like this:
Row Names | Col1 | Col2 | Col3 | Col4 | Col5 |
---|---|---|---|---|---|
Presence | Supreme | Good | Poor | Awful | Worst |
Manner | Good | Awful | Moderate | Moderate | Supreme |
Expression | Awful | Poor | Poor | Awful | Awful |
Complexity | Poor | Good | Worst | Good | Good |
Tradition | Supreme | Moderate | Moderate | Moderate | Poor |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,问题发生在这一行。
shuffled_arcana
是一个因子向量,但您不能拥有因子矩阵,因此它将向量从因子更改为字符,因此排序不会按预期进行。这是一种方法 -
如果您想更改可以使用的特定行 -
In your code, the issue is happening at this line.
shuffled_arcana
is a factored vector but you cannot have a factor-matrix so it changes the vector from factor to character and hence, sorting does not happen as desired.Here's a way -
If you want to change a specific row you may use -
如果我理解正确的话,您想按所选行对矩阵的列进行排序。如果是这样,我想你可能会这样做:
注意,因子中的顺序是由提供级别的顺序给出的。
ordered=TRUE
参数实际上进一步区分了序数尺度和名义尺度。数据:
If I understand you correctly, you want to sort the columns of the matrix by a chosen row. If so, I think you simply may do:
Note, that the order in a
factor
is given by the order how the levels are provided.ordered=TRUE
argument actually further differentiates ordinal scale from nominal scale.Data: