【问题标题】:how to order a list( shapiro test results) in R?如何在 R 中订购列表(夏皮罗测试结果)?
【发布时间】:2021-05-06 22:18:48
【问题描述】:

我需要帮助来排序列表中的结果。下面是一个与我的数据相似的可重现样本:

da.ma <-matrix(1:22000, 10, 22) ## a sample matrix

n <-seq(max(length(da.ma[1,]))) ## naming cols and rows
for (i in n) {
    c.names <- paste("k", n, sep = "")
}
colnames(da.ma) <- c.names 

n.pdf <-seq(length(da.ma[,1]))
for (i in n.pdf) {
    r.names <- paste("text",n.pdf, sep ="")
}
rownames(da.ma) <- r.names
col.names <-names(da.ma[1,])

da.ma <-cbind(id =seq(length(da.ma[,1])), da.ma) ##adding the id col
library(tibble)
data <- as_tibble(da.ma)

library(rstatix)
in.anova <- data %>%  ## in-put data for anova & shapiro tests
  gather(key = "L", value = "V", all_of(col.names)) %>%
  convert_as_factor(id, L)


library(rstatix)  ##running the test
norm_sapiro <-in.anova %>%      
  group_by(L) %>%
  shapiro_test(V)

问题来了:

norm_sapiro

# A tibble: 22 x 4
   L     variable statistic     p
   <fct> <chr>        <dbl> <dbl>
 1 k1    V            0.970 0.892 ##the 1st  1000
 2 k10   V            0.970 0.892 ##the 10th 1000
 3 k11   V            0.970 0.892 ##the 11th 1000
 4 k12   V            0.970 0.892
 5 k13   V            0.970 0.892
 6 k14   V            0.970 0.892
 7 k15   V            0.970 0.892
 8 k16   V            0.970 0.892
 9 k17   V            0.970 0.892
10 k18   V            0.970 0.892
# ... with 12 more rows

我需要按顺序排列关卡 (L) — 这意味着关卡名称的数字部分需要按顺序排列。换句话说,我需要根据从k1 开始的级别对行进行排序。我想要的结果如下所示:

    # A tibble: 22 x 4
       L     variable statistic     p
       <fct> <chr>        <dbl> <dbl>
     1 k1    V            0.970 0.892
     2 k2    V            0.970 0.892
     3 k3    V            0.970 0.892
     4 k4    V            0.970 0.892
     5 k5    V            0.970 0.892
     6 k6    V            0.970 0.892
     7 k7    V            0.970 0.892
     8 k8    V            0.970 0.892
     9 k9    V            0.970 0.892
    10 k10   V            0.970 0.892
    11 k11   V            0.970 0.892
    12 k12   V            0.970 0.892
    13 k13   V            0.970 0.892
    14 k14   V            0.970 0.892
    # ... with 8 more rows

如何将结果按顺序排列(k1、k2、k3、k4、k5...k22)。请注意,我也需要相应的值。

另外,我需要Ls 在绘制绘图时按顺序排列。为上述数据运行此代码(查看 X 轴)

ggboxplot(in.anova, x = "L", y = "V", add = "point")

【问题讨论】:

  • "convert_as_factor(., id, L) 中的错误:找不到函数 "convert_as_factor""
  • @IRTFM 我的错!它在图书馆(rstatix)中。现在就来看看吧。

标签: r list anova normal-distribution


【解决方案1】:

您可以使用stringr::str_sortL 转换为因子,然后排序:

df %>% 
  mutate(L = factor(L, str_sort(L, numeric = T))) %>% 
  arrange(L)

或者readr::parse_number:

df[order(readr::parse_number(df$L)),]

如果L 这么简单,那么您可以简单地提取数字并执行以下操作:

df[order(as.numeric(gsub("k", "", df$L))),] # gsub("\\D+", "", df$L) also works

【讨论】:

  • tnx 很多。当我画一个情节时,我有同样的问题。我需要 Ls 在 X 轴上按顺序排列。我更新了问题。
  • 如果您将L 设置为一个因素,那么它应该以正确的顺序绘制。如果颠倒了,请考虑将其包装在forcats::fct_rev 中或将排序从升序更改为降序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 2021-05-12
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
相关资源
最近更新 更多