r - Combine a specific row from many data frames into one data frame -


i have 20 data frames (dat.table1 dat.table20) this:

> dat.table1             mean         sd          lb         ub 1  -3.251915678 0.09831336 -3.44979982 -3.0579865 2   0.529393596 0.09403571  0.34492156  0.7138352 3   0.437666296 0.09555116  0.25218768  0.6230282 4   0.386773612 0.09338021  0.20630132  0.5708987 5   0.259218892 0.10023005  0.06538325  0.4610775 6  -0.048387041 0.07875680 -0.20517662  0.1020621 7   0.086933460 0.08688864 -0.08462830  0.2565562 8   0.206235709 0.08200178  0.04710170  0.3658142 9   0.343474976 0.08204759  0.18539931  0.5062159 10 -0.354694572 0.08556581 -0.52609169 -0.1916891 11 -0.270542304 0.07349095 -0.41319234 -0.1291315 12  0.124547080 0.08323933 -0.04331230  0.2836064 13  0.005354652 0.10487004 -0.20677503  0.2061523 14  0.296131787 0.08235691  0.13605602  0.4593168 15  0.246056104 0.07536908  0.09803849  0.3959664 16  0.271052276 0.08347047  0.10437983  0.4354910 17 -0.005474416 0.09352408 -0.19415321  0.1736560  > dat.table2           mean         sd          lb         ub 1  -3.32373198 0.10477638 -3.53563786 -3.1241599 2   0.58316739 0.09466424  0.39814125  0.7690037 3   0.47869295 0.09768017  0.28395734  0.6701996 4   0.44479756 0.09489120  0.26172536  0.6336547 5   0.30072454 0.09964341  0.10674064  0.4980277 6  -0.05397720 0.07987092 -0.20952979  0.1038290 7   0.06624190 0.08466350 -0.10406855  0.2297836 8   0.18411601 0.07997405  0.02953943  0.3433614 9   0.35256600 0.07871029  0.20079165  0.5111548 10 -0.39566218 0.08567173 -0.56842809 -0.2281193 11 -0.29250153 0.07652253 -0.44428227 -0.1435696 12  0.07428006 0.08742497 -0.09829608  0.2419713 13 -0.03926006 0.11335154 -0.26894891  0.1716172 14  0.30625276 0.08212213  0.14760732  0.4674057 15  0.26511644 0.07824379  0.11330060  0.4216398 16  0.25476552 0.08699879  0.08646282  0.4240095 17 -0.05081449 0.10151042 -0.25162773  0.1451824 

my question how pick specific row (say row 1) data frames , combine them rows in new data frame?

thanks.

it better read datasets in list rather creating/reading 20 datasets in global enviroment , these kind of operations. having created datasets, do:

lst <- mget(ls(pattern='^dat.table\\d+')) res <- do.call(`rbind`,lapply(lst,function(x) x[1,]))  row.names(res) <- null 

for two datasets, get

res #      mean         sd        lb        ub #1 -3.251916 0.09831336 -3.449800 -3.057987 #2 -3.323732 0.10477638 -3.535638 -3.124160 

another option use slice dplyr

library(dplyr) library(tidyr)  d1 <- unnest(lst, grp) group_by(d1, grp) %>%                   slice(1) #       grp      mean         sd        lb        ub #1 dat.table1 -3.251916 0.09831336 -3.449800 -3.057987 #2 dat.table2 -3.323732 0.10477638 -3.535638 -3.124160 

or using data.table

library(data.table) rbindlist(map(cbind, grp=seq_along(lst), lst))[, head(.sd,1), by=grp] #   grp      mean         sd        lb        ub #1:   1 -3.251916 0.09831336 -3.449800 -3.057987 #2:   2 -3.323732 0.10477638 -3.535638 -3.124160 

update

regaring error message, suspect column names in of lst elements different. example if change

 colnames(lst[[1]])[1] <- "mean1"  do.call(`rbind`,lapply(lst,function(x) x[1,]))  #error in match.names(clabs, names(xi)) :   #names not match previous names 

one option change column names same if columns ordered each dataset

  nm1 <- sapply(lst, function(x) colnames(x))[,2] #because changed 1st element   #column name   lst1 <- lapply(lst, function(x) {colnames(x) <- nm1; x} )   res <- do.call(`rbind`,lapply(lst1,function(x) x[1,]))   row.names(res) <- null 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -