Home > database >  fill list of list by summing rows in each data frame in all combinations
fill list of list by summing rows in each data frame in all combinations

Time:03-11

I have 4 data frames saved as .csv files or .txt (df1.csv,df2.csv,df3.txt,df4.txt). I would like to sum all the rows of df1 with df3 and df4 and of df2 with df3 and df4 in a loop. I would like to store the results in two separate sublists (one for df1 and one for df2) of a main list.

Example:

df1      df3
colA    colA
5        1
1        4
3        1

df2     df4
colA    colA
0        0
2        0
1        9

Output:

I would like to have a list_ALL which contains listDF1 with the results of the sum of df1 with df3 and df4 and a listDF2 which contains results of df2 with df3 and df4

LISTDF1


 df5   df6
   
 colA   colA
  6      5
  5      1
  4      12

LISTDF2


  df7   df8

  colA  colA
   1    0
   6    2
   2    10


list_ALL<-list()

files.csv<-list.files(pattern = "*.csv")
files.txt<-list.files(pattern = "*.txt")
for (i in 1:length(files.csv)) {
  list_ALL[[i]]<-list()
}
names(listALL)<-files.csv

for (i in 1:length(files.csv))
  for (j in 1:length(files.txt))
    {{ list_ALL[[i]][[j]] <- rowSums([[i]][[j]])}}

I tried this however, only the first one of the sublist gets filled up.

CodePudding user response:

Something like this?

lst1 <- list(df1<-c(5,1,3), df2<-c(0,2,1))

lst2 <- list(df3<-c(1,4,1), df4<-c(0,0,9))

res_lst <- list()
for(i in seq_along(lst1)){
  for(j in seq_along(lst2)){
       res <- lst1[[i]] lst2[[j]]
       res_lst <- append(res_lst, list(res))
  }
}

splt_lst <- split(x = res_lst, f = rep(1:2, each=2))


$`1`
$`1`[[1]]
[1] 6 5 4

$`1`[[2]]
[1]  5  1 12


$`2`
$`2`[[1]]
[1] 1 6 2

$`2`[[2]]
[1]  0  2 10

CodePudding user response:

for (i in 1:length(files)){
  for (j in 1:length(meta_exp_dat))
    {
cog_out_dat[[i]][[j]] <- lapply(files,function(x) try(read_outcome_data(
  snps = meta_exp_dat[[j]]$SNP,
  filename = x,
  gene_col = "anno",
  sep = " ",
  chr_col = "CHR",
  pos_col = "BP",
  snp_col = "SNP",
  beta_col = "BETA",
  se_col = "SE",
  effect_allele_col = "ALLELE1",
  other_allele_col = "ALLELE0",
  eaf_col = "EAF",
  pval_col = "P",
  phenotype_col = "DATA"
)))
}}
  • Related