list variables to individual data.frames - r

Let's say I have a list of 30 data.frames, each containing 2 variables (called value, and rank), called myList
I'd know I can use
my.DF <- do.call("cbind", myList)
to create the output my.DF containing all the variables next to each other.
It is possible to cbind each variable individually into it's own data.frame i.e to just have a new data.frame of just the 2nd variable?

We can extract the second column by looping over the list (lapply) and wrap with data.frame.
data.frame(lapply(myList, `[`, 2))
If we want to separate the variables,
lapply(names(myList[[1]]), function(x)
do.call(cbind,lapply(myList, `[`, x)))
data
set.seed(24)
myList <- list( data.frame(value=1:6, rank= sample(6)),
data.frame(value=7:12, rank=sample(6)))

Related

RowMeans for a column across a large list of lists of dataframes in R

I have a foreach loop that produces a list of dataframes within the loop for each thread. It then summarises those lists into a 'large list' of lists as in the code below.
I want to find the RowMeans for each row in the 'Pdist' column across all the dataframes.
How do I access the dataframes when they are so deeply embedded in a list structure?
Here is some example data. Note the dataframes do repeat, but that is just for illustration purposes.
list_1 <- list()
dataframe_1 <- data.frame(ind_comp_a = c("OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2645ii_dPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_dPOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2413iiiaMOU","OP5046___eWAT","OP2413iiicMOU","OP2413iiibMOU","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___eWAT","OP5048___bPHA","OP5046___eWAT","OP2645ii_aPOR","OP5046___dWAT","OP5046___aWAT","OP5046___aWAT","OP2645ii_aPOR","OP5048___fPHA","OP2645ii_aPOR","OP5046___dWAT","OP2645ii_ePOR","OP2645ii_dPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_bPOR","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___aWAT","OP2645ii_dPOR","OP5048___bPHA","OP5048___fPHA","OP5046___dWAT","OP5046___eWAT","OP5046___eWAT","OP2645ii_aPOR","OP2645ii_ePOR","OP2645ii_dPOR","OP5048___bPHA","OP2645ii_ePOR","OP2645ii_ePOR","OP2645ii_bPOR","OP2645ii_ePOR","OP2645ii_aPOR","OP5048___ePHA","OP5046___eWAT","OP2645ii_cPOR","OP2645ii_dPOR","OP2645ii_cPOR","OP5048___fPHA","OP2645ii_dPOR","OP2645ii_dPOR","OP2645ii_ePOR","OP2645ii_bPOR","OP5048___ePHA","OP5046___eWAT","OP2645ii_ePOR","OP2645ii_ePOR","OP5046___aWAT","OP2645ii_aPOR","OP5044___cMOU","OP2645ii_cPOR","OP2645ii_ePOR","OP5046___dWAT","OP2645ii_dPOR","OP5046___aWAT","OP5046___bWAT","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___bWAT","OP5053DNAaPHA","OP5048___ePHA","OP2645ii_dPOR","OP5048___dPHA","OP5046___bWAT","OP5046___aWAT","OP2645ii_ePOR","OP2413iiibMOU","OP2413iiicMOU","OP2645ii_dPOR","OP5044___cMOU","OP2645ii_bPOR","OP2645ii_aPOR","OP2413iiiaMOU","OP2645ii_dPOR","OP5046___bWAT","OP5046___dWAT","OP2645ii_aPOR","OP5048___bPHA","OP5051DNAbCOM","OP5046___dWAT","OP2413iiibMOU","OP2413iiicMOU","OP2645ii_bPOR","OP5049___bWAT","OP5046___aWAT","OP2413iiiaMOU","OP5046___cWAT","OP5046___bWAT","OP5046___bWAT","OP2645ii_dPOR","OP2645ii_bPOR","OP5043___aWAT","OP5048___fPHA","OP2645ii_dPOR","OP5046___bWAT","OP5046___eWAT","OP5048___hPHA","OP5046___bWAT","OP5048___hPHA","OP2645ii_ePOR","OP5048___cPHA","OP5046___cWAT","OP5048___ePHA","OP5048___ePHA","OP5046___bWAT","OP5046___cWAT","OP2645ii_aPOR","OP2645ii_ePOR","OP2645ii_aPOR","OP5048___bPHA","OP5048___cPHA","OP5043___bWAT","OP5046___dWAT","OP5048___hPHA","OP2645ii_cPOR","OP5048___fPHA","OP5048___bPHA","OP2645ii_dPOR","OP2645ii_bPOR","OP5048___dPHA","OP5053DNAaPHA","OP5048___fPHA","OP2645ii_aPOR","OP2645ii_ePOR","OP5044___cMOU","OP5046___eWAT","OP2645ii_bPOR","OP2645ii_bPOR","OP5048___fPHA","OP5044___cMOU","OP2645ii_ePOR"),
ind_comp_b = c("OP5048___bPHA","OP5051DNAbCOM","OP5046___bWAT","OP5048___fPHA","OP5043___bWAT","OP5043___aWAT","OP5047___bPHA","OP5052DNAaWAT","OP5053DNAcPHA","OP5048___ePHA","OP5049___aWAT","OP5046___cWAT","OP5053DNAaPHA","OP5046___eWAT","OP5044___aMOU","OP5051DNAaCOM","OP5048___cPHA","OP5048___gPHA","OP5054DNAbMOU","OP5048___bPHA","OP5048___hPHA","OP5047___aPHA","OP5053DNAbPHA","OP5051DNAcCOM","OP5048___fPHA","OP5046___aWAT","OP5048___bPHA","OP5049___bWAT","OP5048___fPHA","OP5048___bPHA","OP5048___ePHA","OP5044___bMOU","OP3088i__aPOR","OP5046___dWAT","OP5048___fPHA","OP5054DNAcMOU","OP5048___ePHA","OP2645ii_cPOR","OP5048___fPHA","OP2645ii_cPOR","OP2645ii_cPOR","OP5051DNAbCOM","OP5053DNAaPHA","OP5048___bPHA","OP5054DNAaMOU","OP5048___ePHA","OP5043___aWAT","OP5048___bPHA","OP5048___bPHA","OP5048___fPHA","OP5046___bWAT","OP5054DNAaMOU","OP5043___bWAT","OP5048___fPHA","OP5053DNAaPHA","OP5048___ePHA","OP5048___cPHA","OP5044___cMOU","OP5048___bPHA","OP5048___hPHA","OP5047___bPHA","OP5048___ePHA","OP5051DNAbCOM","OP5049___bWAT","OP5049___bWAT","OP5048___ePHA","OP5053DNAaPHA","OP5048___hPHA","OP5053DNAcPHA","OP5043___aWAT","OP5046___bWAT","OP5054DNAcMOU","OP5048___cPHA","OP5048___hPHA","OP5048___fPHA","OP5051DNAbCOM","OP5047___aPHA","OP5054DNAaMOU","OP5048___cPHA","OP5048___dPHA","OP5053DNAaPHA","OP5054DNAaMOU","OP5054DNAcMOU","OP5043___aWAT","OP5043___bWAT","OP5043___bWAT","OP2645ii_cPOR","OP5049___bWAT","OP5047___aPHA","OP5047___aPHA","OP5046___bWAT","OP5053DNAaPHA","OP5052DNAaWAT","OP5048___bPHA","OP2645ii_ePOR","OP5047___bPHA","OP5053DNAaPHA","OP5047___bPHA","OP5048___hPHA","OP5048___dPHA","OP5048___gPHA","OP5049___aWAT","OP5048___ePHA","OP5054DNAaMOU","OP5054DNAcMOU","OP5048___cPHA","OP5051DNAbCOM","OP5048___fPHA","OP5048___cPHA","OP5053DNAcPHA","OP5048___bPHA","OP5048___bPHA","OP5048___hPHA","OP5048___fPHA","OP5048___ePHA","OP5053DNAbPHA","OP5048___bPHA","OP5053DNAcPHA","OP5048___hPHA","OP5048___hPHA","OP5046___cWAT","OP5048___dPHA","OP5054DNAaMOU","OP5048___cPHA","OP5048___fPHA","OP5048___fPHA","OP5051DNAbCOM","OP5053DNAaPHA","OP5047___aPHA","OP5048___fPHA","OP5048___fPHA","OP5048___cPHA","OP5048___bPHA","OP5047___aPHA","OP5046___bWAT","OP5054DNAaMOU","OP5052DNAaWAT","OP5052DNAaWAT","OP5047___aPHA","OP5048___dPHA","OP5049___bWAT","OP5054DNAaMOU","OP5054DNAaMOU","OP5048___gPHA","OP5054DNAaMOU","OP5048___bPHA","OP5051DNAbCOM","OP5052DNAaWAT","OP5053DNAaPHA","OP5048___ePHA","OP5051DNAaCOM","OP5053DNAbPHA","OP5044___aMOU","OP5051DNAcCOM","OP5049___bWAT","OP5054DNAaMOU","OP5047___aPHA","OP5051DNAbCOM","OP2645ii_dPOR","OP5051DNAcCOM","OP5052DNAaWAT","OP5049___aWAT","OP5053DNAaPHA","OP5048___fPHA","OP5054DNAcMOU","OP5051DNAbCOM","OP5054DNAbMOU","OP5052DNAaWAT","OP5048___ePHA","OP5053DNAbPHA","OP5043___bWAT","OP5043___aWAT","OP5049___aWAT","OP5051DNAbCOM","OP5049___aWAT"),
Pdist = c(0.12653736,0.12545262,0.12409420,0.12023167,0.11852507,0.11574044,0.11371805,0.11165877,0.11096499,0.11000436,0.10860921,0.10716355,0.10648404,0.10457088,0.10043985,0.10043419,0.09902992,0.09809625,0.09742466,0.09706079,0.09691789,0.09532336,0.09374877,0.09359057,0.09352572,0.09191749,0.09136457,0.08965083,0.08872891,0.08630526,0.08531594,0.08454861,0.08453494,0.08312192,0.08258318,0.08140542,0.08140466,0.08083571,0.08036883,0.07964833,0.07964736,0.07930556,0.07916955,0.07909909,0.07871759,0.07749702,0.07735318,0.07692221,0.07663146,0.07655228,0.07610728,0.07601355,0.07589804,0.07586683,0.07475816,0.07427158,0.07295387,0.07264578,0.07239881,0.07239652,0.07230148,0.07213147,0.07178486,0.07143912,0.07102923,0.07034595,0.07017927,0.07009262,0.06990277,0.06953688,0.06945218,0.06933059,0.06923690,0.06918330,0.06905105,0.06894675,0.06886782,0.06873706,0.06835633,0.06827398,0.06818929,0.06815169,0.06781528,0.06755839,0.06709807,0.06673160,0.06651507,0.06631521,0.06577319,0.06527915,0.06521944,0.06479374,0.06450183,0.06444880,0.06439217,0.06363232,0.06313289,0.06312447,0.06301823,0.06299480,0.06277461,0.06277369,0.06274871,0.06205441,0.06190890,0.06190525,0.06183778,0.06180255,0.06174675,0.06142775,0.06142015,0.06141977,0.06132026,0.06126746,0.06121289,0.06106807,0.06069853,0.06060409,0.06057873,0.06002828,0.05988876,0.05983741,0.05952482,0.05916929,0.05912005,0.05911979,0.05906816,0.05899453,0.05865145,0.05853252,0.05818659,0.05785562,0.05784148,0.05781387,0.05760903,0.05755058,0.05742954,0.05731918,0.05701451,0.05701384,0.05698890,0.05686745,0.05665475,0.05662290,0.05661457,0.05648999,0.05641717,0.05638154,0.05633743,0.05630275,0.05624860,0.05594854,0.05594397,0.05581496,0.05577077,0.05576073,0.05571763,0.05552730,0.05545187,0.05541380,0.05508725,0.05495578,0.05481013,0.05478274,0.05476202,0.05470291,0.05452429,0.05403781,0.05369966,0.05355532,0.05337705,0.05334701,0.05318317,0.05289062,0.05281420))
list_1[[1]] <- dataframe1
dataframe2 <- data.frame(ind_comp_a = c("OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2645ii_dPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_cPOR","OP2645ii_dPOR","OP2645ii_cPOR","OP2645ii_ePOR","OP2413iiiaMOU","OP5046___eWAT","OP2413iiicMOU","OP2413iiibMOU","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___eWAT","OP5048___bPHA","OP5046___eWAT","OP2645ii_aPOR","OP5046___dWAT","OP5046___aWAT","OP5046___aWAT","OP2645ii_aPOR","OP5048___fPHA","OP2645ii_aPOR","OP5046___dWAT","OP2645ii_ePOR","OP2645ii_dPOR","OP2645ii_aPOR","OP2645ii_cPOR","OP2645ii_bPOR","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___aWAT","OP2645ii_dPOR","OP5048___bPHA","OP5048___fPHA","OP5046___dWAT","OP5046___eWAT","OP5046___eWAT","OP2645ii_aPOR","OP2645ii_ePOR","OP2645ii_dPOR","OP5048___bPHA","OP2645ii_ePOR","OP2645ii_ePOR","OP2645ii_bPOR","OP2645ii_ePOR","OP2645ii_aPOR","OP5048___ePHA","OP5046___eWAT","OP2645ii_cPOR","OP2645ii_dPOR","OP2645ii_cPOR","OP5048___fPHA","OP2645ii_dPOR","OP2645ii_dPOR","OP2645ii_ePOR","OP2645ii_bPOR","OP5048___ePHA","OP5046___eWAT","OP2645ii_ePOR","OP2645ii_ePOR","OP5046___aWAT","OP2645ii_aPOR","OP5044___cMOU","OP2645ii_cPOR","OP2645ii_ePOR","OP5046___dWAT","OP2645ii_dPOR","OP5046___aWAT","OP5046___bWAT","OP2645ii_aPOR","OP2645ii_aPOR","OP5046___bWAT","OP5053DNAaPHA","OP5048___ePHA","OP2645ii_dPOR","OP5048___dPHA","OP5046___bWAT","OP5046___aWAT","OP2645ii_ePOR","OP2413iiibMOU","OP2413iiicMOU","OP2645ii_dPOR","OP5044___cMOU","OP2645ii_bPOR","OP2645ii_aPOR","OP2413iiiaMOU","OP2645ii_dPOR","OP5046___bWAT","OP5046___dWAT","OP2645ii_aPOR","OP5048___bPHA","OP5051DNAbCOM","OP5046___dWAT","OP2413iiibMOU","OP2413iiicMOU","OP2645ii_bPOR","OP5049___bWAT","OP5046___aWAT","OP2413iiiaMOU","OP5046___cWAT","OP5046___bWAT","OP5046___bWAT","OP2645ii_dPOR","OP2645ii_bPOR","OP5043___aWAT","OP5048___fPHA","OP2645ii_dPOR","OP5046___bWAT","OP5046___eWAT","OP5048___hPHA","OP5046___bWAT","OP5048___hPHA","OP2645ii_ePOR","OP5048___cPHA","OP5046___cWAT","OP5048___ePHA","OP5048___ePHA","OP5046___bWAT","OP5046___cWAT","OP2645ii_aPOR","OP2645ii_ePOR","OP2645ii_aPOR","OP5048___bPHA","OP5048___cPHA","OP5043___bWAT","OP5046___dWAT","OP5048___hPHA","OP2645ii_cPOR","OP5048___fPHA","OP5048___bPHA","OP2645ii_dPOR","OP2645ii_bPOR","OP5048___dPHA","OP5053DNAaPHA","OP5048___fPHA","OP2645ii_aPOR","OP2645ii_ePOR","OP5044___cMOU","OP5046___eWAT","OP2645ii_bPOR","OP2645ii_bPOR","OP5048___fPHA","OP5044___cMOU","OP2645ii_ePOR"),
ind_comp_b = c("OP5048___bPHA","OP5051DNAbCOM","OP5046___bWAT","OP5048___fPHA","OP5043___bWAT","OP5043___aWAT","OP5047___bPHA","OP5052DNAaWAT","OP5053DNAcPHA","OP5048___ePHA","OP5049___aWAT","OP5046___cWAT","OP5053DNAaPHA","OP5046___eWAT","OP5044___aMOU","OP5051DNAaCOM","OP5048___cPHA","OP5048___gPHA","OP5054DNAbMOU","OP5048___bPHA","OP5048___hPHA","OP5047___aPHA","OP5053DNAbPHA","OP5051DNAcCOM","OP5048___fPHA","OP5046___aWAT","OP5048___bPHA","OP5049___bWAT","OP5048___fPHA","OP5048___bPHA","OP5048___ePHA","OP5044___bMOU","OP3088i__aPOR","OP5046___dWAT","OP5048___fPHA","OP5054DNAcMOU","OP5048___ePHA","OP2645ii_cPOR","OP5048___fPHA","OP2645ii_cPOR","OP2645ii_cPOR","OP5051DNAbCOM","OP5053DNAaPHA","OP5048___bPHA","OP5054DNAaMOU","OP5048___ePHA","OP5043___aWAT","OP5048___bPHA","OP5048___bPHA","OP5048___fPHA","OP5046___bWAT","OP5054DNAaMOU","OP5043___bWAT","OP5048___fPHA","OP5053DNAaPHA","OP5048___ePHA","OP5048___cPHA","OP5044___cMOU","OP5048___bPHA","OP5048___hPHA","OP5047___bPHA","OP5048___ePHA","OP5051DNAbCOM","OP5049___bWAT","OP5049___bWAT","OP5048___ePHA","OP5053DNAaPHA","OP5048___hPHA","OP5053DNAcPHA","OP5043___aWAT","OP5046___bWAT","OP5054DNAcMOU","OP5048___cPHA","OP5048___hPHA","OP5048___fPHA","OP5051DNAbCOM","OP5047___aPHA","OP5054DNAaMOU","OP5048___cPHA","OP5048___dPHA","OP5053DNAaPHA","OP5054DNAaMOU","OP5054DNAcMOU","OP5043___aWAT","OP5043___bWAT","OP5043___bWAT","OP2645ii_cPOR","OP5049___bWAT","OP5047___aPHA","OP5047___aPHA","OP5046___bWAT","OP5053DNAaPHA","OP5052DNAaWAT","OP5048___bPHA","OP2645ii_ePOR","OP5047___bPHA","OP5053DNAaPHA","OP5047___bPHA","OP5048___hPHA","OP5048___dPHA","OP5048___gPHA","OP5049___aWAT","OP5048___ePHA","OP5054DNAaMOU","OP5054DNAcMOU","OP5048___cPHA","OP5051DNAbCOM","OP5048___fPHA","OP5048___cPHA","OP5053DNAcPHA","OP5048___bPHA","OP5048___bPHA","OP5048___hPHA","OP5048___fPHA","OP5048___ePHA","OP5053DNAbPHA","OP5048___bPHA","OP5053DNAcPHA","OP5048___hPHA","OP5048___hPHA","OP5046___cWAT","OP5048___dPHA","OP5054DNAaMOU","OP5048___cPHA","OP5048___fPHA","OP5048___fPHA","OP5051DNAbCOM","OP5053DNAaPHA","OP5047___aPHA","OP5048___fPHA","OP5048___fPHA","OP5048___cPHA","OP5048___bPHA","OP5047___aPHA","OP5046___bWAT","OP5054DNAaMOU","OP5052DNAaWAT","OP5052DNAaWAT","OP5047___aPHA","OP5048___dPHA","OP5049___bWAT","OP5054DNAaMOU","OP5054DNAaMOU","OP5048___gPHA","OP5054DNAaMOU","OP5048___bPHA","OP5051DNAbCOM","OP5052DNAaWAT","OP5053DNAaPHA","OP5048___ePHA","OP5051DNAaCOM","OP5053DNAbPHA","OP5044___aMOU","OP5051DNAcCOM","OP5049___bWAT","OP5054DNAaMOU","OP5047___aPHA","OP5051DNAbCOM","OP2645ii_dPOR","OP5051DNAcCOM","OP5052DNAaWAT","OP5049___aWAT","OP5053DNAaPHA","OP5048___fPHA","OP5054DNAcMOU","OP5051DNAbCOM","OP5054DNAbMOU","OP5052DNAaWAT","OP5048___ePHA","OP5053DNAbPHA","OP5043___bWAT","OP5043___aWAT","OP5049___aWAT","OP5051DNAbCOM","OP5049___aWAT"),
Pdist = c(0.12653736,0.12545262,0.12409420,0.12023167,0.11852507,0.11574044,0.11371805,0.11165877,0.11096499,0.11000436,0.10860921,0.10716355,0.10648404,0.10457088,0.10043985,0.10043419,0.09902992,0.09809625,0.09742466,0.09706079,0.09691789,0.09532336,0.09374877,0.09359057,0.09352572,0.09191749,0.09136457,0.08965083,0.08872891,0.08630526,0.08531594,0.08454861,0.08453494,0.08312192,0.08258318,0.08140542,0.08140466,0.08083571,0.08036883,0.07964833,0.07964736,0.07930556,0.07916955,0.07909909,0.07871759,0.07749702,0.07735318,0.07692221,0.07663146,0.07655228,0.07610728,0.07601355,0.07589804,0.07586683,0.07475816,0.07427158,0.07295387,0.07264578,0.07239881,0.07239652,0.07230148,0.07213147,0.07178486,0.07143912,0.07102923,0.07034595,0.07017927,0.07009262,0.06990277,0.06953688,0.06945218,0.06933059,0.06923690,0.06918330,0.06905105,0.06894675,0.06886782,0.06873706,0.06835633,0.06827398,0.06818929,0.06815169,0.06781528,0.06755839,0.06709807,0.06673160,0.06651507,0.06631521,0.06577319,0.06527915,0.06521944,0.06479374,0.06450183,0.06444880,0.06439217,0.06363232,0.06313289,0.06312447,0.06301823,0.06299480,0.06277461,0.06277369,0.06274871,0.06205441,0.06190890,0.06190525,0.06183778,0.06180255,0.06174675,0.06142775,0.06142015,0.06141977,0.06132026,0.06126746,0.06121289,0.06106807,0.06069853,0.06060409,0.06057873,0.06002828,0.05988876,0.05983741,0.05952482,0.05916929,0.05912005,0.05911979,0.05906816,0.05899453,0.05865145,0.05853252,0.05818659,0.05785562,0.05784148,0.05781387,0.05760903,0.05755058,0.05742954,0.05731918,0.05701451,0.05701384,0.05698890,0.05686745,0.05665475,0.05662290,0.05661457,0.05648999,0.05641717,0.05638154,0.05633743,0.05630275,0.05624860,0.05594854,0.05594397,0.05581496,0.05577077,0.05576073,0.05571763,0.05552730,0.05545187,0.05541380,0.05508725,0.05495578,0.05481013,0.05478274,0.05476202,0.05470291,0.05452429,0.05403781,0.05369966,0.05355532,0.05337705,0.05334701,0.05318317,0.05289062,0.05281420))
list_1[[2]] <- dataframe2
list_1[[3]] <- dataframe1
list_1[[4]] <- dataframe2
list_2 <- list()
list_2[[1]] <- dataframe1
list_2[[2]] <- dataframe2
list_2[[3]] <- dataframe1
list_2[[4]] <- dataframe2
Large_list <- list(list_1,list_2)
Many Thanks.
Cheers.
Deon
From my understanding you want the row means for the "Pdist" column in dataframes in a nested list with two levels (dataframes being in the second level). In that case, you can do:
rowMeans(do.call(cbind, lapply(unlist(Large_list, recursive = F), `[[`, "Pdist")))
Use unlist() w/ recursive = FALSE to make remove a level and
Use lapply() to extract the "Pdist" columns,
Usedo.call() with cbind() to bind them together
Then rowMeans()

How to cbind many data frames with a loop?

I have 105 data frames with xts, zoo class and II want to combine their 6th columns into a data frame.
So, I created a data frame that contains all the data frame names to use it with a 'for' function:
mydata <- AAL
for (i in 2:105) {
k <- top100[i,1] # The first column contains all the data frame names
mydata <- cbind(mydata, k)
}
It's obviously wrong, but I have no idea either how to cbind so many data frames with completely different names (my data frame names are NASDAQ Symbols) nor how to pick the 6th column of all.
Thank you in advance
Try foreach package. May be there is more elegant way to do this task, but this approach will work.
library(foreach)
#create simple data frames with columns named 'A' and 'B'
df1<-t(data.frame(1,2,3))
df2<-t(data.frame(4,5,6))
colnames(df1)<-c('A')
colnames(df2)<-c('B')
#make a list
dfs<-list(df1,df2)
#join data frames column by column, this will preserve their names
foreach(x=1:2
,.combine=cbind)%do% # don`t forget this directive
{
dfs[[x]]
}
The result will be:
A B
X1 1 4
X2 2 5
X3 3 6
To pick column number 6:
df[,6]
First, you should store all of your data.frames in a list. You can then use a combination of lapply and do.call to extract and recombine the sixth columns of each of the data.frames:
# Create sample data
df_list <- lapply(1:105, function(x) {
as.data.frame(matrix(sample(1:1000, 100), ncol = 10))
})
# Extract the sixth column from each data.frame
extracted_cols <- lapply(df_list, function(x) x[6])
# Combine all of the columns together into a new data.frame
result <- do.call("cbind", extracted_cols)
One way to get all of your preexisting data.frames into a list would be to use lapply along with get:
df_list <- lapply(top100[[1]], get)

R convert data.frame to list by column

I would like to convert a data.frame into a list of data.frames by column using base R functions and holding the first column constant. For example, I would like to split DF into a list of three data.frames, each of which includes the first column. That is, I would like to end up with the list named LONG without having to type out each list element out separately. Thank you.
DF <- data.frame(OBS=1:10,HEIGHT=rnorm(10),WEIGHT=rnorm(10),TEMP=rnorm(10))
DF
LONG <- list(HEIGHT = DF[c("OBS", "HEIGHT")],
WEIGHT = DF[c("OBS", "WEIGHT")],
TEMP = DF[c("OBS", "TEMP" )])
LONG
SHORT <- as.list(DF)
SHORT
SPLIT <- split(DF, col(DF))
We can loop through the names of 'DF' except the first one, cbind the first column with the subset of 'DF' from the names.
setNames(lapply(names(DF)[-1], function(x) cbind(DF[1], DF[x])), names(DF)[-1])
Or another option would be
Map(cbind, split.default(DF[-1], names(DF)[-1]), OBS=DF[1])

Select a numeric columns of a dataframe in a list

I have a list of dataframes. After applying a function I get new columns that are non numeric. From each resulting dataframe that I save in a list modified_list As a result I want to save my modified dataframes but I only want to save the columns that contain numeric values.
I am stocked in the selection of numeric columns. I do not know how to select numeric columns on a list of dataframes. My code looks something like this. Please do you have any idea what can i do to make this code work?
library(plyr)
library(VIM)
data1 <- sleep
data2 <- sleep
data3 <- sleep
# get a list of dataframes
list_dataframes <- list(data1, data2, data3) # list of dataframes
n <- length(list_dataframes)
# apply function to the list_dataframes
modified_list <- llply(list_dataframes, myfunction)
# selects only numeric results
nums <- llply(modified_list, is.numeric)
# saving results
for (i in 1:n){
write.table(file = sprintf( "myfile/%s_hd.txt", dataframes[i]), modified_list[[i]][, nums], row.names = F, sep=",")
}
It sounds like you want to subset each data.frame in a list of data.frames to their numeric columns.
You can test which columns of a data.frame called df are numeric with
sapply(df, is.numeric)
This returns a logical vector, which can be used to subset your data.frame like this:
df[sapply(df, is.numeric)]
Returning the numeric columns of that data.frame. To do this over a list of data.frames df_list and return a list of subsetted data.frames:
lapply(df_list, function(df) df[sapply(df, is.numeric)])
Edit: Thanks #Richard Scriven for simplifying suggestion.

creat a new variable within several data frames in R

I have several data frames df1, df, 2...., df10. Columns (variables) are the same in all of them.
I want to create a new variable within each of them. I can easily do it "manually" as follows:
df1$newvariable <- ifelse(df1$oldvariable == 999, NA, df1$oldvariable)
or, alternatively
df1 = transform(df1, df1$newvariable= ifelse(df1$oldvariable==999, NA, df1$oldvariable)))
Unfortunately I'm not able to do this in a loop. If I write
for (i in names) { #names is the list of dataframes
i$newvariable <- ifelse(i$oldvariable == 999, NA, i$oldvariable)
}
I get the following output
Error in i$oldvariable : $ operator is invalid for atomic vectors
What I'd do is to pool all data.frame on to a list and then use lapply as follows:
df1 <- as.data.frame(matrix(runif(2*10), ncol=2))
df2 <- as.data.frame(matrix(runif(2*10), ncol=2))
df3 <- as.data.frame(matrix(runif(2*10), ncol=2))
df4 <- as.data.frame(matrix(runif(2*10), ncol=2))
# create a list and use lapply
df.list <- list(df1, df2, df3, df4)
out <- lapply(df.list, function(x) {
x$id <- 1:nrow(x)
x
})
Now, you'll have all the data.frames with a new column id appended and out is a list of data.frames. You can access each of the data.frames with x[[1]], x[[2]] etc...
This has been asked many times. The $<- is not capable of translating that "i" index into either the first or second arguments. The [[<- is capable of doing so for the second argument but not the first. You should be learning to use lapply and you will probably need to do it with two nested lapply's, one for the list of "names" and the other for each column in the dataframes. The question is incomplete since it lacks specific examples. Make up a set of three dataframes, set some of the values to "999" and provide a list of names.

Resources