Unless you really want to, it's a bad idea to create all these new varaibles based on the elements in input$name
because:
if the
input$name
contains a name such as 'input' that clashes with another variable you can get bugs that are hard to track downYou potentially clutter up your workspace with many variables
It's hard(er) to loop over the variables to plot them without using esoteric bits of R code, or copying and pasting lots of code.
I suggest creating a list as follows:
rows <- 1:nrow(input)
res <- lapply(which(input$FC>=0.7),function(x) {
x2 <- rows
x2<- input$chr[x2] == input$chr[x] & abs(input$Position[x2] - input$Position[x]) < 2500
input[x2,]})
names(res) <- input$Name[input$FC>=0.7] ##corrected this line
where each element of the list is one of the variables that you wanted to create. Access as res[["BD13"]]
or res[[1]]
- the latter form will make it easy to produce all your plots in a loop.
Edit:
To plot, I think that you want the following (can't test at the moment):
for (i in 1:length(res)) {
pdf(sprintf("%s.pdf", names(res)[i]))
boxplot(res[[i]]$Name, res[[i]]$FC, xlab=res[[i]]$Name, ylab="FC", main=names(res)[i])
dev.off()
}
but check the arguments to boxplot - I don't think the first one should be the text
res[i]
is a list (of length 1) containing the i
th element of res
, whereas res[[i]]
is the i
th element itself.