假设我们有这两个列表:列出一个
c1 <- matrix(rnorm(10),5,2)
c2 <- c1+(rnorm(10))
c3 <- c1+(rnorm(10))
c4 <- c1+(rnorm(10))
c5 <- c1+(rnorm(10))
c6 <- c1+(rnorm(10))
c7 <- c1+(rnorm(10))
c8 <- c1+(rnorm(10))
c9 <- c1+(rnorm(10))
c10 <- c1+(rnorm(10))
c11 <- c1+(rnorm(10))
c12<- c1+(rnorm(10))
c13<- c1+(rnorm(10))
c14 <- c1+(rnorm(10))
c15 <- c1+(rnorm(10))
c16 <- c1+(rnorm(10))
c17 <- c1+(rnorm(10))
c18 <- c1+(rnorm(10))
clist <- list(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18)
[[1]]
[,1] [,2]
[1,] 1.0700367 -0.5578757
[2,] 0.4093932 1.0749494
[3,] 1.1078936 -1.0020313
[4,] -0.7811367 -0.9588735
[5,] -0.7702231 -0.4892086
[[2]]
[,1] [,2]
[1,] 1.57797328 0.2617391
[2,] -0.87241974 1.1265720
[3,] 1.84947367 -0.7139085
[4,] -1.63986024 -0.9102586
[5,] 0.06221699 -0.6733914
[[3]]
[,1] [,2]
[1,] 2.289326 -0.77862436
[2,] -1.022810 1.11059490
[3,] 1.036657 0.06886355
[4,] -1.383734 -0.99963748
[5,] -2.141255 -1.29084565
[[4]]
[,1] [,2]
[1,] 0.5070891 -0.45562637
[2,] 1.0324446 1.41556011
[3,] 2.4718491 -1.39879189
[4,] -0.6604285 -0.47753910
[5,] 0.7938556 -0.07936295
[[5]]
[,1] [,2]
[1,] 3.3870090 0.5420060
[2,] 0.5712447 1.3975933
[3,] 1.3072899 -0.6763534
[4,] 0.5605419 -0.4463263
[5,] 0.7240062 -0.3466927
[[6]]
[,1] [,2]
[1,] 2.31201726 -1.0508046
[2,] -0.06529705 2.3007519
[3,] 1.01335486 0.2294716
[4,] 0.30277459 -0.9007592
[5,] -1.01667980 -0.2653819
[[7]]
[,1] [,2]
[1,] 0.72524700 -1.1381316
[2,] -0.70805611 1.4912682
[3,] 0.02581648 -0.1799978
[4,] -0.69420585 -2.0897702
[5,] 0.28893614 -1.3320487
[[8]]
[,1] [,2]
[1,] 1.0035684 -1.7132670
[2,] 0.4127813 1.5093681
[3,] 1.3565442 -1.3588956
[4,] -2.8350358 -1.0903141
[5,] -2.4558814 -0.9655129
[[9]]
[,1] [,2]
[1,] 1.08047188 -0.8820000
[2,] -0.19398400 -0.2308767
[3,] 0.09548916 -1.3369467
[4,] -0.68126015 -1.9699754
[5,] 0.49449792 0.4916281
[[10]]
[,1] [,2]
[1,] -0.2658601 -2.2107617
[2,] -0.1627423 1.4692983
[3,] 3.4493613 -1.3103298
[4,] -0.3894654 -1.4713198
[5,] 0.8240821 -0.2250143
[[11]]
[,1] [,2]
[1,] 2.9858303 0.3477594
[2,] -1.5882846 0.5219365
[3,] 1.5934080 -1.7183402
[4,] -0.9507265 -0.9574800
[5,] -0.9013779 -0.8767775
[[12]]
[,1] [,2]
[1,] 0.7939398 -2.018252
[2,] 0.6124091 2.571972
[3,] 1.9311252 -1.533483
[4,] -0.1643360 -2.611893
[5,] -0.6491992 -1.580492
[[13]]
[,1] [,2]
[1,] 1.1829224 -1.0877178
[2,] 1.6820163 1.3020885
[3,] 0.3300722 -0.5611728
[4,] -1.7108015 -1.5658648
[5,] -0.5647340 -0.4141432
[[14]]
[,1] [,2]
[1,] 2.19552384 -1.0960342
[2,] 1.15353759 0.5215864
[3,] -0.48139839 0.1075568
[4,] -0.93807917 -2.6135192
[5,] 0.01308484 -0.4473025
[[15]]
[,1] [,2]
[1,] 1.3210720 0.5649543
[2,] 2.0667212 1.0788922
[3,] 1.4608440 -0.4755102
[4,] -1.7570569 -2.4803738
[5,] -0.6317204 -0.3547752
[[16]]
[,1] [,2]
[1,] -0.8485633 -0.3334503
[2,] 0.6451695 0.7377303
[3,] -0.4772256 -0.2918937
[4,] -0.7817388 -1.2121758
[5,] -0.2658948 -1.2121535
[[17]]
[,1] [,2]
[1,] 1.1988099 -3.2396990
[2,] 1.9654758 -0.3612864
[3,] 1.1614214 -0.8344391
[4,] -0.5958542 -1.7135859
[5,] -1.0568211 -0.7607357
[[18]]
[,1] [,2]
[1,] 2.0347305 -1.5804555
[2,] -0.6078962 0.7648275
[3,] 1.2695973 -0.4216392
[4,] -1.8700421 0.6019247
[5,] -1.1738761 -1.3271278
清单2:
a1 <- matrix(rnorm(10),5,2)
a2 <- c1+(rnorm(10))
a3 <- c1+(rnorm(10))
alist <- list(a1,a2,a3)
[[1]]
[,1] [,2]
[1,] 1.0700367 -0.5578757
[2,] 0.4093932 1.0749494
[3,] 1.1078936 -1.0020313
[4,] -0.7811367 -0.9588735
[5,] -0.7702231 -0.4892086
[[2]]
[,1] [,2]
[1,] 1.57797328 0.2617391
[2,] -0.87241974 1.1265720
[3,] 1.84947367 -0.7139085
[4,] -1.63986024 -0.9102586
[5,] 0.06221699 -0.6733914
[[3]]
[,1] [,2]
[1,] 2.289326 -0.77862436
[2,] -1.022810 1.11059490
[3,] 1.036657 0.06886355
[4,] -1.383734 -0.99963748
[5,] -2.141255 -1.29084565
我想在第一个列表中的每6个矩阵后插入第二个列表中的矩阵,所以我的模式为c1 c2 c3 c4 c5 c6 a1 c7 c8 c9 c10 c11 c12 a2 c13 c14 c15 c16 c17 c18 a3
我有以下代码:
names(clist) <- paste0("c", seq_along(clist))
names(alist) <- paste0("c", seq_along(alist)*6, "a")
x <- c(clist, alist)
x <- x[order(names(x))]
因此,在运行x <- x[order(names(x))]
之前,names(x)
看起来像这样:
[1] "c1" "c2" "c3" "c4" "c5" "c6" "c7" "c8" "c9" "c10" "c11"
[12] "c12" "c13" "c14" "c15" "c16" "c17" "c18" "c6a" "c12a" "c18a"
然而,在运行x <- x[order(names(x))]
后,列表中的矩阵顺序变为:
[1] "c1" "c10" "c11" "c12" "c12a" "c13" "c14" "c15" "c16" "c17" "c18"
[12] "c18a" "c2" "c3" "c4" "c5" "c6" "c6a" "c7" "c8" "c9"
我需要它
[1] "c1" "c2" "c3" "c4" "c5" "c6" "c6a" "c7" "c8" "c9" "c10" "c11"
[12] "c12" "c12a" "c13" "c14" "c15" "c16" "c17" "c18" "c18a"
如果矩阵的数量小于10,那么代码将完美地运行。但是,R不认为c10
大于c2
。
我认为此问题的解决方案是从包sortedc <- mixedsort(sort(names(x)))
运行gtools
,names(x)
按所需顺序对x <- x[order(sortedc)]
进行排序,然后运行PackageImports
,但它不起作用。
答案 0 :(得分:1)
尝试使用purrr
包:
library(purrr)
clist_split <-
split(clist, gl(length(clist) / 6, 6)) # split the list into chunks of 6
alist <- map(alist, list) # make elements of alist lists themselves
temp <- map2(clist_split, alist, append) # append one element of alist to each 6 lists in each element of clist_split
result <- unlist(temp, recursive = FALSE) # flatten list by one level
但请注意,对我来说,alist <- list(c1,c2,c3)
似乎有一个拼写错误。不应该是alist <- list(a1,a2,a3)
吗?
答案 1 :(得分:1)
我会使用for
循环。这是一个使用for
循环和append
函数的基本R方法。
# construct data
set.seed(1234)
cMat <- matrix(rnorm(10),5,2)
clist <- replicate(18, cMat + rnorm(10), simplify=FALSE)
# values to insert into list
myVals <- list("snack", "time", "now")
根据这些数据,构建一个插入索引,然后运行循环。
# construct index for placement
idx <- seq(7, 21, 7)
# run loop
for(i in seq_along(idx)) clist <- append(clist, myVals[[i]], idx[i]-1L)
让我们检查第一个插入的值
clist[[7]]
[1] "snack"
答案 2 :(得分:1)
由于您说sortedc
符合所需的顺序,因此您可以通过以下方式获得所需的结果:
x <- x[sortedc]
"["
函数将接受名称向量作为参数,并对其元素进行选择或重新排序。注意:我认为你只需要:
sortedc <- mixedsort( names(x))
以下是代码测试:
> library(gtools)
> cMat <- matrix(rnorm(10),5,2); clist <- replicate(21, cMat + rnorm(10), simplify=FALSE)
> names(clist) <- c( paste0("c", 1:18), paste0("c", seq(6,18,by=6), "a") )
> x <- clist[ mixedsort(names(clist) ) ]
> names(x)
[1] "c1" "c2" "c3" "c4" "c5" "c6" "c6a" "c7" "c8" "c9" "c10" "c11" "c12" "c12a"
[15] "c13" "c14" "c15" "c16" "c17" "c18" "c18a"