Extracting individual columns of data in R

Viewed 49

I have a simulation algorithm that I studied to compare the performance of multiple estimators for a two-parameter distribution, where tout is the object of the simulation and the results for the first 6 iterations are given as:

head(tout)
[[1]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.5386432 0.4382962 0.5099481 0.4715912 0.5117606 0.4602767 0.4651867 0.4988287 0.5257753 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.4541418 0.5647531 0.4279015 0.5084405 0.4425654 

[[2]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.6440018 0.3967015 0.5765640 0.4677472 0.6514293 0.3987146 0.5970640 0.4396663 0.6687088 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.3915725 0.6977988 0.3744663 0.6168053 0.4001208 

[[3]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.7026951 0.4104054 0.5068797 0.5920761 0.7095411 0.4512077 0.5186618 0.5788831 0.7413820 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.4389810 0.9342899 0.3370598 0.6385650 0.4243651 

[[4]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.5978735 0.5018492 0.5209601 0.5734814 0.6299665 0.4931704 0.5557625 0.5426690 0.6490281 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.4872303 0.6688643 0.4783249 0.5608085 0.5056437 

[[5]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.6342671 0.3089608 0.5868977 0.3669342 0.6292674 0.3223752 0.5960829 0.3499934 0.6436543 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.3149113 0.6847822 0.2869070 0.6003836 0.3170046 

[[6]]
     MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1     WLSE2      CVM1 
0.6470335 0.3835832 0.6044660 0.4289784 0.6264162 0.4015191 0.5790582 0.4369397 0.6436909 
     CVM2     MCVM1     MCVM2      MPS1      MPS2 
0.3942025 0.6852247 0.3690787 0.6126173 0.3894142 

I would like to extract all values for MLE1, MLE2, and so on. I tried using tout[1,], but it did not work. The error message it gave was Error in tout[1, ] : incorrect number of dimensions. How do I extract all values separately for MLE1, MLE2, and so on?

1 Answers

You can use sapply with [:

l <- replicate(3, c(MLE1 = 0.641151978310245,  MLE2 = 0.343605418474544, MOME1 = 0.589030092164895, MOME2 = 0.402978104488642, OLSE1 = 0.592058622388682, OLSE2 = 0.391744158767641, WLSE1 = 0.555834606688624, WLSE2 = 0.421744980944362, CVM1 = 0.607818252992196, CVM2 = 0.384273611960049, MCVM1 = 0.698076643246126, MCVM2 = 0.323239571664426, MPS1 = 0.615145435338788, MPS2 = 0.347714221623107),
          simplify = F)
 #mock data set

t(sapply(l, `[`, seq(length(l[[1]]))))

#          MLE1      MLE2     MOME1     MOME2     OLSE1     OLSE2     WLSE1
# [1,] 0.641152 0.3436054 0.5890301 0.4029781 0.5920586 0.3917442 0.5558346
# [2,] 0.641152 0.3436054 0.5890301 0.4029781 0.5920586 0.3917442 0.5558346
# [3,] 0.641152 0.3436054 0.5890301 0.4029781 0.5920586 0.3917442 0.5558346
#         WLSE2      CVM1      CVM2     MCVM1     MCVM2      MPS1      MPS2
# [1,] 0.421745 0.6078183 0.3842736 0.6980766 0.3232396 0.6151454 0.3477142
# [2,] 0.421745 0.6078183 0.3842736 0.6980766 0.3232396 0.6151454 0.3477142
# [3,] 0.421745 0.6078183 0.3842736 0.6980766 0.3232396 0.6151454 0.3477142
Related