【发布时间】: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