Search code examples
rlistanovanormal-distribution

how to order a list( shapiro test results) in R?


I need help with ordering the results that are in a list. Below is a reproducible sample that is similar to my data:

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)

Here comes the problem:

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

I need the levels (L) to be in order —meaning the numeric parts of the level names need to be in order. In other words, I need to order the rows based on the levels that start from k1. My desired out come would look like this:

    # 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

How can I put the results in the order(k1, k2, k3, k4, k5...k22). Please note that I need the corresponding values to be in order as well.

Also, I need Ls to be in order when I draw a plot. Run this code for the above data( have a look at X-axis)

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

Solution

  • You can convert L to a factor using stringr::str_sort and then sort:

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

    Or with readr::parse_number:

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

    If L is that simple then you can simply extract the digits and do:

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