Reputation: 725
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.
Upvotes: 1
Views: 74
Reputation: 725
for (j in 1:length(files))
{
cog_out_dat[[j]]<- lapply(meta_exp_dat,function(df) try(read_outcome_data(
snps = df$SNP,
filename = files[[j]],
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"
)))
}
Upvotes: 0
Reputation: 25353
Another possible solution:
set.seed(123)
df1 <- data.frame(colA = sample(1:10, 3))
df2 <- data.frame(colA = sample(1:10, 3))
df3 <- data.frame(colA = sample(1:10, 3))
df4 <- data.frame(colA = sample(1:10, 3))
lapply(list(df1, df2),
\(x) asplit(sapply(list(df3, df4), \(y) rowSums(cbind(x,y))), 2))
#> [[1]]
#> [[1]][[1]]
#> [1] 8 14 8
#>
#> [[1]][[2]]
#> [1] 9 19 4
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] 7 10 9
#>
#> [[2]][[2]]
#> [1] 8 15 5
Upvotes: 1
Reputation: 1312
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
Upvotes: 1