在附表中,我想将变量X的值和变量Y\X=8的值从低到高重新排序,也就是说,变量如下:6,8,9和10
这些是我用来生成表格的代码,称为“dataP”:
lim <- sample(2:10, 4)
lim1 <- sample(3:10, 4)
create_table <- function() {
n_a <- sample(10:25,1)
n_b <- 30-n_a
n_c <- sample(5:18,1)
n_d <- 20-n_c
P3 <- rmultinom(1, n_a, prob=c(0.2, 0.4, 0.3, 0.1))
P4 <- rmultinom(1, n_b, prob=c(0.1, 0.2, 0.4, 0.3))
P5 <- rmultinom(1, n_a, prob=c(0.2, 0.4, 0.3, 0.1))
P6 <- rmultinom(1, n_b, prob=c(0.1, 0.2, 0.4, 0.3))
matrix(c(P3, P4, P5, P6), nrow = 4, ncol = 4,
dimnames = list(c(lim[1], lim[2] ,lim[3],lim[4]),
c(lim1[1] ,lim1[2], lim1[3], lim1[4])))
}
public <- create_table()
r <- sample(1:4,1)
dataP <- data.frame(c(lim1[1], lim1[2], lim1[3], lim1[4], "Suma"),
c(public[r,1], public[r,2], public[r,3], public[r,4],public[r,1]+ public[r,2]+ public[r,3]+public[r,4]))
colnames (dataP) <-lapply(list( "Y\\X={states[[1]][r]}","n[i/j == paste( {ansord})]" ),glue)
如果您有能力,我建议您先对其进行排序,然后再选择删除第一列中的数字并转换为字符串。
dataP <- data.frame("Y\\X=8" = c(9,8,6,10), "n[i/j==3]" = c(6,3,6,3), check.names=FALSE)
dataP <- dataP[order(dataP$`Y\\X=8`),]
rbind(dataP, data.frame("Y\\X=8"="Suma", "n[i/j==3]"=18, check.names=FALSE))
# Y\\X=8 n[i/j==3]
# 3 6 6
# 2 8 3
# 1 9 6
# 4 10 3
# 11 Suma 18
但是,如果您无法返回并修复该过程,您可以通过暂时将它们重新转换回数字,然后根据它们进行排序来修复它。
dataP <- data.frame("Y\\X=8" = c(9,8,6,10,"Suma"), "n[i/j==3]" = c(6,3,6,3,18), check.names=FALSE)
dataP[order(suppressWarnings(as.numeric(dataP$`Y\\X=8`)), na.last = TRUE),]
# Y\\X=8 n[i/j==3]
# 3 6 6
# 2 8 3
# 1 9 6
# 4 10 3
# 5 Suma 18