按因子级别的顺序对数据框中行内的值进行排序?

发布于 2025-01-16 00:05:31 字数 2309 浏览 4 评论 0原文

因此,我正在尝试使用 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")

大部分代码正在工作,并且它产生的输出看起来有点像这样:

行名称Col1Col2Col3Col4Col5
状态糟糕差 最高最差中等
态度好 糟糕中等最高表达糟糕
方式可怕糟糕复杂
最坏
传统最高中等中等中等

但是,无论我做什么,我似乎都无法对其进行正确排序。我尝试过 order(),我尝试过 sort(),我查看了 StackExchange 上可以看到的所有代码,但找不到任何有帮助的内容。充其量,它什么也没做;最坏的情况是,它不仅无法排序,而且还删除了因子值,以便仅显示基础数字。

例如,一旦在“存在”行上排序,它应该看起来像这样:

行名称Col1Col2Col3Col4Col5
PresenceSupremeGoodPoorAwfulWorst
MannerGoodAwfulModerateModerateSupreme
ExpressionAwfulPoorPoorAwfulAwful
ComplexityPoorGoodWorstGoodGood
TraditionSupreme中等中等中等

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 NamesCol1Col2Col3Col4Col5
PresenceAwfulGoodPoorSupremeWorst
MannerGoodAwfulModerateModerateSupreme
ExpressionAwfulPoorPoorAwfulAwful
ComplexityPoorGoodWorstGoodGood
TraditionSupremeModerateModerateModeratePoor

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 NamesCol1Col2Col3Col4Col5
PresenceSupremeGoodPoorAwfulWorst
MannerGoodAwfulModerateModerateSupreme
ExpressionAwfulPoorPoorAwfulAwful
ComplexityPoorGoodWorstGoodGood
TraditionSupremeModerateModerateModeratePoor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2025-01-23 00:05:31

在您的代码中,问题发生在这一行。

arcana_table <- as.data.frame(matrix(shuffled_arcana, nrow = 5, ncol = 5))

shuffled_arcana 是一个因子向量,但您不能拥有因子矩阵,因此它将向量从因子更改为字符,因此排序不会按预期进行。

这是一种方法 -

set.seed(2022)

arcanaVector <- c(rep("Supreme", 3),
                  rep(c("Good", "Moderate", "Poor", "Awful"), each = 5),
                  rep("Worst", 2))
arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
shuffled_arcana <- sample(arcanaVector)
arcana_table <- matrix(shuffled_arcana,nrow = 5, ncol = 5)
row.names(arcana_table) <- c("Presence", "Manner", "Expression", "Complexity", "Tradition")

arcana_table <- apply(arcana_table, 1, function(x) sort(factor(x, arcanaLevels))) |>
  t() |>
  as.data.frame()

arcana_table

#                 V1       V2       V3       V4    V5
#Presence       Good     Good     Good     Good Awful
#Manner      Supreme Moderate     Poor    Awful Awful
#Expression  Supreme  Supreme Moderate Moderate  Poor
#Complexity Moderate Moderate     Poor     Poor Worst
#Tradition      Good     Poor    Awful    Awful Worst

如果您想更改可以使用的特定行 -

arcana_table[1, ] <- as.character(sort(factor(arcana_table[1, ], arcanaLevels))) 

In your code, the issue is happening at this line.

arcana_table <- as.data.frame(matrix(shuffled_arcana, nrow = 5, ncol = 5))

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 -

set.seed(2022)

arcanaVector <- c(rep("Supreme", 3),
                  rep(c("Good", "Moderate", "Poor", "Awful"), each = 5),
                  rep("Worst", 2))
arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
shuffled_arcana <- sample(arcanaVector)
arcana_table <- matrix(shuffled_arcana,nrow = 5, ncol = 5)
row.names(arcana_table) <- c("Presence", "Manner", "Expression", "Complexity", "Tradition")

arcana_table <- apply(arcana_table, 1, function(x) sort(factor(x, arcanaLevels))) |>
  t() |>
  as.data.frame()

arcana_table

#                 V1       V2       V3       V4    V5
#Presence       Good     Good     Good     Good Awful
#Manner      Supreme Moderate     Poor    Awful Awful
#Expression  Supreme  Supreme Moderate Moderate  Poor
#Complexity Moderate Moderate     Poor     Poor Worst
#Tradition      Good     Poor    Awful    Awful Worst

If you want to change a specific row you may use -

arcana_table[1, ] <- as.character(sort(factor(arcana_table[1, ], arcanaLevels))) 
绮筵 2025-01-23 00:05:31

如果我理解正确的话,您想按所选行对矩阵的列进行排序。如果是这样,我想你可能会这样做:

chosen <- 'Presence'
arcana_table[, order(factor(arcana_table[chosen, ], levels=arcanaLevels))]
#            [,1]       [,2]       [,3]       [,4]    [,5]      
# Presence   "Supreme"  "Moderate" "Poor"     "Poor"  "Poor"    
# Manner     "Awful"    "Worst"    "Good"     "Worst" "Awful"   
# Expression "Awful"    "Poor"     "Supreme"  "Poor"  "Good"    
# Complexity "Moderate" "Awful"    "Moderate" "Good"  "Good"    
# Tradition  "Moderate" "Supreme"  "Good"     "Awful" "Moderate"

chosen <- 'Manner'
arcana_table[, order(factor(arcana_table[chosen, ], levels=arcanaLevels))]
#            [,1]       [,2]       [,3]       [,4]    [,5]   
# Presence   "Poor"     "Supreme"  "Poor"     "Poor"  "Moderate"
# Manner     "Good"     "Awful"    "Awful"    "Worst" "Worst"   
# Expression "Supreme"  "Awful"    "Good"     "Poor"  "Poor"    
# Complexity "Moderate" "Moderate" "Good"     "Good"  "Awful"   
# Tradition  "Good"     "Moderate" "Moderate" "Awful" "Supreme" 

注意,因子中的顺序是由提供级别的顺序给出的。 ordered=TRUE 参数实际上进一步区分了序数尺度和名义尺度。


数据:

arcanaVector <- structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L), .Label = c("Supreme", 
"Good", "Moderate", "Poor", "Awful", "Worst"), class = c("ordered", 
"factor"))

set.seed(42)  ## for sake of reproducibility

arcana_table <- matrix(sample(arcanaVector), nrow=5, ncol=5,
                       dimnames=list(c("Presence", "Manner", "Expression",
                                       "Complexity", "Tradition"), NULL)) 

arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")

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:

chosen <- 'Presence'
arcana_table[, order(factor(arcana_table[chosen, ], levels=arcanaLevels))]
#            [,1]       [,2]       [,3]       [,4]    [,5]      
# Presence   "Supreme"  "Moderate" "Poor"     "Poor"  "Poor"    
# Manner     "Awful"    "Worst"    "Good"     "Worst" "Awful"   
# Expression "Awful"    "Poor"     "Supreme"  "Poor"  "Good"    
# Complexity "Moderate" "Awful"    "Moderate" "Good"  "Good"    
# Tradition  "Moderate" "Supreme"  "Good"     "Awful" "Moderate"

chosen <- 'Manner'
arcana_table[, order(factor(arcana_table[chosen, ], levels=arcanaLevels))]
#            [,1]       [,2]       [,3]       [,4]    [,5]   
# Presence   "Poor"     "Supreme"  "Poor"     "Poor"  "Moderate"
# Manner     "Good"     "Awful"    "Awful"    "Worst" "Worst"   
# Expression "Supreme"  "Awful"    "Good"     "Poor"  "Poor"    
# Complexity "Moderate" "Moderate" "Good"     "Good"  "Awful"   
# Tradition  "Good"     "Moderate" "Moderate" "Awful" "Supreme" 

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:

arcanaVector <- structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L), .Label = c("Supreme", 
"Good", "Moderate", "Poor", "Awful", "Worst"), class = c("ordered", 
"factor"))

set.seed(42)  ## for sake of reproducibility

arcana_table <- matrix(sample(arcanaVector), nrow=5, ncol=5,
                       dimnames=list(c("Presence", "Manner", "Expression",
                                       "Complexity", "Tradition"), NULL)) 

arcanaLevels <- c("Supreme", "Good", "Moderate", "Poor", "Awful", "Worst")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文