Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Extracting data from a list of vector in the while loop in r [closed]

$
0
0

I post my follow up question about my previous one. From this code, I want a list of vector to be created containing data with this format: l3 [[1]] [1] v1 v2 v3 v4

[[2]] [1] w1 w2 w3 w4

[[3]] [1] z1 z2 z3 z4

But what I got was like this: Seems like it keep repeating elements redundantly in a list. print(l3)

[[1]] [1] 100.00000 99.45692 98.91680 98.37960 101.04577 100.49701 99.95124 99.06635 98.52834 97.99326 100.14615 99.60228 99.06137

[[2]] [1] 100.00000 99.45692 98.91680 98.37960 101.04577 100.49701 99.95124 99.06635 98.52834 97.99326 100.14615 99.60228 99.06137 [14] 99.99692 99.45386 98.91375 99.15577 98.61728 98.08171 98.73635 98.20013 97.66683 99.45615 98.91603 98.37884

[[3]] [1] 100.00000 99.45692 98.91680 98.37960 101.04577 100.49701 99.95124 99.06635 98.52834 97.99326 100.14615 99.60228 99.06137 [14] 99.99692 99.45386 98.91375 99.15577 98.61728 98.08171 98.73635 98.20013 97.66683 99.45615 98.91603 98.37884 100.71692 [27] 100.16995 99.62595 96.63577 96.11096 95.58901 98.29635 97.76252 97.23160 98.53615 98.00103 97.46881

I have no idea how to make it work. Anyone to reply or comment is always appreciated.

     ml<-list(c(-0.00543076923076923, 0.0104576923076923, -0.00933653846153846, 
                  0.00146153846153846), 
                c(-3.07692307692302e-05, -0.00844230769230769, -0.0126365384615385, 
                  -0.00543846153846154), 
                c(0.00716923076923077, -0.0336423076923077, -0.0170365384615385, 
                 -0.0146384615384615))

       f2<-function(x) x+1
       l2<-lapply(ml,f2)
       l2

       l3 <- vector("list", 3)
       g<-c(100)

       for(i in 1:3){
           for(j in 1:4){
              k=2
              while(k<= 4){

                g<- c(g, (g[k-1]*l2[[i]][j]))
                l3[[i]]<- g 
                k<-k+1
            }

          }

      }

print(l3)


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>