Reputation: 61
Sorry about the probable misleading title and possible post duplication
I want to create a loop that will parse a list elements 1:4 and its one be set in a data.frame with the name of the element chosen by the loop. I am starting with a list:
> names(peakAnnoList_chip)
[1] "CAPAN1_px8_JunB" "CAPAN1_px13_JunB" "Panc1_13_cjun" "Panc1_px8_old_cJun"
> class(peakAnnoList_chip)
[1] "list"
then I get the first element:
> CAPAN1_px8_JunB <- as.data.frame(peakAnnoList_chip[[1]]@anno)
> class(CAPAN1_px8_JunB)
[1] "data.frame"
So that in the end I can do:
write.table(CAPAN1_px8_JunB,
file = "CAPAN1_px8_JunB_annotation.txt",
sep = "\t",
quote = F,
row.names = F)
but there I can not find an elegant way to set the specific name (CAPAN1_px8_JunB) to the data.frame
any clues?
Upvotes: 1
Views: 56
Reputation: 886948
One option is lapply
to loop over the names of the 'peakAnnoList_chip', extract the 'anno' and write it to different datasets with write.table
lapply(names(peakAnnoList_chip), function(nm)
write.table(peakAnnoList_chip[[nm]]@anno,
file = paste0(nm, "_annotation.txt"), sep="\t", quote = FALSE))
Or another option is Map
Map(function(dat, nm) write.table(dat@nno, file =
paste0(nm, "_annotation.txt"), sep="\t", quote = FALSE),
peakAnnoList_chip, names(peakAnnoList_chip))
Or with imap
library(purrr)
library(stringr)
imap(peakAnnoList_chip, ~ write.table(.x, file = str_c(.y,
"_annotation.txt"), sep="\t", quote = FALSE))
Upvotes: 1