I have a dataframe of genetic data called 'windows'
glimpse(windows)
Rows: 3,000
Columns: 5
$ chrpos <chr> "1:104159999-104168402", "1:104159999-104168402", "1:104159999-104168402"
$ target <chr> "AMY2A", "CFD13", "PUTA"
$ name <chr> "rs5753", "rs70530", "rs21111"
$ chr <chr> "1", "1", "1"
$ pos <int> 104560629, 104562750, 104557705
I want to make them in to separate dataframes by the row in the column 'target'
AMY2A<- filter(windows, target == 'AMY2A')
write.table("am2ya.txt", header=T)
Which works fine, but is laborious doing for each element row of 'target'
But how do I loop through all of these, and then save them out as text files, in one go?
list <- windows$target
for(i in list)
df.list[[i]]<-filter(windows, target == list[[i]])
Which gives the error:
Error in `filter()`:
! Problem while computing `..1 = target == list[[i]]`.
Caused by error in `list[[i]]`:
! subscript out of bounds
And when I google saving out multiple txt files, it just comes up how to read in multiple text files.
Any help would be great, thanks!
CodePudding user response:
Or, using the tidyverse, something like
windows %>%
group_by(target) %>%
group_walk(
function(.x, .y) {
write.table(.x, paste0(tolower(.y$target[1]), ".txt"))
},
.keep=TRUE
)
[Untested code, since your question is not reproducible.]
the .keep=TRUE
keeps the target
column in the text file. If that's unnecessary, feel free to delete it. Details are in the online doc.
CodePudding user response:
probably something like:
lst <- windows$target
for(i in seq_along(lst)){
dat <- filter(windows, target == lst[[i]])
write.table(dat, paste0(tolower(lst[[i]]), ".txt"))
}