dataframe creation from list in R

55 views Asked by At

I have a list in R environment named Vars, its a list of 20. Each Vars, i=1,2,...,10. have an output similar to this:

     var co
1 (Intercept) 2.446776739
2          x2 1.483926503
3          x8 0.379060372
4         x10 2.330205729
5         x15 1.399578214

I want to create a dataframe that contains as output for each Xi

where j is the coefficients as displayed in Vars and each zet belongs to the coresponding variable Xi

>zet
                   x1         x2          x3         x4         x5          x6         x7         x8          x9        x10
        1 0.009704237 0.08319483 0.006421977 0.07528413 0.01538562 0.009653445 0.02759767 0.01827908 0.009425397 0.01119717
             x11        x12        x13        x14        x15        x16       x17        x18       x19        x20
    1 0.01777308 0.02659095 0.01598912 0.01195379 0.01881498 0.02790549 0.0312646 0.01879999 0.0108261 0.01627832

I believe that lapply is the way to go but i can't figure out how to code it in R.

2

There are 2 answers

7
Ronak Shah On BEST ANSWER

We can use mapply here :

mapply(function(x, y) x*sum(y$coefficient), lambda, ssVarX)

Similar with purrr's map2_dbl :

purrr::map2_dbl(lambda, ssVarX, ~.x * sum(.y$coefficient))
0
akrun On

We can use lapply from base R

lapply(seq_along(ssVarX), function(i)lambda[i] * sum(ssVarX[[i]]$coefficient))