如何在函数中插入seq()作为条件?

时间:2017-11-19 14:32:09

标签: r list matrix

假设我有这些数据:

set.seed(1234)
cMat <- matrix(rnorm(10),5,2)
clist <- replicate(39, cMat + rnorm(10), simplify=FALSE)

[[1]]
           [,1]       [,2]
[1,] -1.6842584  0.3957704
[2,] -0.7209572 -1.0857495
[3,]  0.3081873 -1.4578273
[4,] -2.2812389 -1.4016237
[5,]  1.3886187  1.5257973

[[2]]
           [,1]          [,2]
[1,] -1.0729775 -9.421490e-01
[2,] -0.2132567  1.576077e-05
[3,]  0.6438933 -1.570288e+00
[4,] -1.8861083 -5.795903e-01
[5,] -0.2645956 -1.825986e+00

[[3]]
           [,1]       [,2]
[1,] -0.1047682 -0.6615634
[2,] -0.1981638 -2.7547796
[3,]  0.3750011 -1.8876250
[4,] -2.8469558 -0.8587459
[5,] -1.1999688 -1.3559354

[[4]]
           [,1]       [,2]
[1,]  0.2424305 -0.4624584
[2,] -0.7912135 -1.6820582
[3,]  0.2290765 -1.7986177
[4,] -2.6263207 -1.0882801
[5,] -0.5652154 -1.3868878

[[5]]
            [,1]      [,2]
[1,] -3.01309701  1.069112
[2,] -0.30464668  1.073078
[3,] -0.02444845 -1.319985
[4,] -3.36065971  1.041458
[5,]  0.26681517 -2.047846

[[6]]
           [,1]       [,2]
[1,] -0.5504773  2.2831403
[2,]  2.8264203 -1.7133477
[3,]  1.0496808  0.8211953
[4,] -3.0153313  0.7651128
[5,]  0.4215199 -0.5535650

[[7]]
           [,1]       [,2]
[1,] -1.2001729  0.3526575
[2,] -0.1780395 -1.9654409
[3,]  0.7179172 -1.2702136
[4,] -1.6974111 -0.3061902
[5,]  2.4993956 -1.2070969

[[8]]
           [,1]        [,2]
[1,] -1.3848557  1.20366460
[2,]  0.1074352 -0.02474261
[3,] -0.2878607 -0.94936383
[4,] -2.5194849 -0.75604577
[5,]  1.2793569 -2.08456571

[[9]]
            [,1]       [,2]
[1,] -1.26022457  0.8616062
[2,]  0.53262524 -1.7093480
[3,]  2.79040518  0.3315718
[4,] -1.34418445  0.4084648
[5,] -0.06645875  1.2310793

[[10]]
           [,1]       [,2]
[1,] -0.7925422  0.6730452
[2,] -0.1972892 -1.4710046
[3,]  1.1504347 -0.3784465
[4,] -2.8481755 -0.2094837
[5,] -0.3968739 -0.9421429

[[11]]
            [,1]       [,2]
[1,] -1.40300037  1.3371965
[2,] -0.37164051 -1.8190278
[3,] -0.02532605 -0.3776054
[4,] -1.49642350  0.1087143
[5,]  0.45148721 -0.9163142

[[12]]
           [,1]       [,2]
[1,] -1.3984579  0.5861155
[2,] -0.5044774 -1.2061493
[3,]  3.1426032 -2.0599200
[4,] -1.5951962 -1.2005518
[5,]  2.2533330 -0.6637363

[[13]]
            [,1]       [,2]
[1,] -0.19337540  0.1402036
[2,]  0.53017938 -0.8908583
[3,] -0.08750714 -2.4948779
[4,] -1.67698337  0.3556055
[5,] -1.22097625 -1.5129094

[[14]]
           [,1]        [,2]
[1,] -1.5411024  0.90532770
[2,]  1.6725771  1.08811649
[3,]  1.7211156 -0.27073845
[4,] -2.4541294 -0.05817938
[5,]  0.9428875 -0.54248585

[[15]]
           [,1]      [,2]
[1,] -1.5843034  1.868187
[2,]  0.3750487 -0.809361
[3,]  2.7231858 -1.600015
[4,] -3.2212902 -1.434236
[5,]  0.5508847 -1.280165

[[16]]
            [,1]       [,2]
[1,] -2.05441582  1.1306890
[2,]  0.01678985  1.1034658
[3,]  0.67002147 -0.6153255
[4,] -2.52874850 -0.8852919
[5,]  0.83618079  0.5809679

[[17]]
           [,1]       [,2]
[1,]  0.4972636 -0.3315265
[2,]  0.3206733 -1.6985028
[3,]  0.7517839  2.4971340
[4,] -4.1679331 -0.3294307
[5,]  1.8403871 -0.9232964

[[18]]
           [,1]       [,2]
[1,] -3.9392853  2.4897881
[2,]  0.1776387  0.5943686
[3,]  2.0604729 -1.0553689
[4,] -1.9318288  0.1397282
[5,]  1.3414469 -1.0884541

[[19]]
           [,1]        [,2]
[1,] -1.7451365  1.00675051
[2,] -2.5783294  0.04547024
[3,]  0.2947943 -1.51253507
[4,] -1.8578831 -0.40179729
[5,]  2.5971572 -2.96827537

[[20]]
           [,1]      [,2]
[1,] -0.7218389  1.266518
[2,]  0.9741980  1.267724
[3,]  1.2699551  0.565731
[4,] -1.6449642 -0.531788
[5,]  0.7408057 -2.004487

[[21]]
           [,1]       [,2]
[1,] -0.7890079  0.9280643
[2,] -0.1228060 -0.7264765
[3,]  2.5779343 -1.1527830
[4,] -3.9527786 -0.8691731
[5,]  0.0133729 -0.2605017

[[22]]
           [,1]       [,2]
[1,] -0.3118938 -0.1537142
[2,]  0.9376419  2.3444002
[3,]  3.3579247  0.1307836
[4,] -1.1722001 -1.2487723
[5,]  0.7168344 -0.7035457

[[23]]
             [,1]       [,2]
[1,] -1.531459050 -0.5714862
[2,]  0.002725024 -3.8078921
[3,]  0.150937836 -0.8015065
[4,] -2.228852358 -0.5349342
[5,]  0.748284927 -0.2957641

[[24]]
           [,1]       [,2]
[1,] -1.1479306  0.7577070
[2,]  0.6908281  0.7825345
[3,] -0.0133310 -0.1421634
[4,] -1.6345224 -0.3000877
[5,]  1.1480134 -0.6219939

[[25]]
           [,1]       [,2]
[1,] -0.7701352  0.1355584
[2,]  1.3375531  0.9022296
[3,]  1.5366316 -1.7705356
[4,] -1.6824991 -0.3063836
[5,] -0.7072489 -0.4850350

[[26]]
            [,1]       [,2]
[1,] -0.23126243 -0.1605091
[2,] -0.07144749 -0.8133866
[3,]  1.24306662 -1.7343971
[4,] -4.10895277 -0.1795167
[5,]  0.76772074 -0.2234583

[[27]]
          [,1]       [,2]
[1,] -1.511680 -0.1450577
[2,]  2.102440  0.2338793
[3,]  1.755001  0.4399488
[4,] -1.397065 -0.5706228
[5,]  2.478528 -0.5709855

[[28]]
           [,1]       [,2]
[1,] -2.2188877  0.8254534
[2,]  0.7475968 -1.4212626
[3,]  0.3834708 -0.7923950
[4,] -1.5320148 -2.1173110
[5,] -0.3823061 -0.7616038

[[29]]
           [,1]       [,2]
[1,] -0.2216224  2.3158613
[2,]  0.4606768 -1.7497767
[3,] -0.6817880 -0.9133351
[4,] -2.9662314 -0.2108266
[5,]  2.0851677 -0.5708816

[[30]]
           [,1]       [,2]
[1,] -1.7870227 -0.1429723
[2,] -0.6758495 -1.0791142
[3,]  0.9050126  1.0677596
[4,] -1.3358895 -1.0114118
[5,]  0.4527513 -0.1268611

[[31]]
            [,1]       [,2]
[1,]  0.26465294  1.7337730
[2,]  0.72109415 -0.7242935
[3,]  0.66271931  1.0033515
[4,] -2.38569933 -1.1260645
[5,] -0.06315528 -1.5371551

[[32]]
           [,1]       [,2]
[1,] -1.0639336 -0.2105208
[2,]  0.3016179  0.5078710
[3,]  0.5799897 -1.4993173
[4,] -3.9270945  0.5620307
[5,]  0.4591911 -1.5390808

[[33]]
           [,1]       [,2]
[1,] -0.9145957 -0.6291570
[2,]  1.1761320 -0.8448196
[3,]  0.5656988  1.0731580
[4,] -1.7912592 -0.7785832
[5,]  0.3411510 -1.7078203

[[34]]
           [,1]      [,2]
[1,] -1.2610887  1.670631
[2,]  0.6075709 -1.339000
[3,]  2.0397658 -2.891145
[4,] -1.2017378 -1.036135
[5,]  0.5296471 -1.405893

[[35]]
          [,1]       [,2]
[1,] -3.523102  1.0535801
[2,]  0.839901  1.3164871
[3,]  0.300666 -1.4247090
[4,] -2.571752 -0.6770109
[5,] -1.157978  1.0586752

[[36]]
            [,1]       [,2]
[1,] -0.27324942  1.9406296
[2,]  2.19048866 -1.8605785
[3,]  1.07920712 -0.2393176
[4,] -2.49795775 -0.6107705
[5,] -0.08050697  1.3618040

[[37]]
           [,1]        [,2]
[1,] -1.8150995  0.63834698
[2,] -1.2318589 -0.85026471
[3,]  1.3170729 -1.22539050
[4,] -2.3853464 -0.06361607
[5,] -0.4100004 -1.22170014

[[38]]
           [,1]       [,2]
[1,] -3.0420460  1.7211095
[2,] -2.3743120  0.4077655
[3,]  0.5038590 -0.2308678
[4,] -0.8915108 -2.0715146
[5,]  1.2672541 -0.6844680

[[39]]
           [,1]        [,2]
[1,]  0.3901623  1.29545030
[2,] -3.1186343 -0.11764045
[3,]  0.3030889 -0.00779874
[4,] -1.2432331 -0.54980888
[5,]  0.9578697 -1.80652696

这个简单的功能

testfunction <- function(X)
{
newX <- X[c(1:5),]*2
return(newX)
}

然后我在此代码中使用它将其应用于列表中的每个矩阵:clistnew <- lapply(clist, testfunction)

我想添加一个条件 - 我希望以下序列中的矩阵行乘以3而不是2:

mysequence = seq(8,length(clist) ,by=13)
[1]  8 21 34

我认为将代码更改为可以正常工作

testfunction <- function(X)
{
newX <- X[c(1:5),]*2
for(i in seq(8,length(X) ,by=13)){
newX <- X[c(1:5),]*3
}
return(newX)
}

但是它将所有矩阵的行乘以3,忽略前面的代码行。我确信解决方案很简单,但我只是一个初学者,不幸的是我无法弄明白。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,以下将做你想要的。

testfunction <- function(i, X, myseq){
    if(i %in% myseq)
        newX <- X[1:5, ]*3
    else
        newX <- X[1:5, ]*2
    newX
}

mysequence = seq(8, length(clist), by = 13)

lapply(seq_along(clist), function(i) testfunction(i, clist[[i]], mysequence))