Writing to a file through a loop

R programming language resources Forums Data manipulation Writing to a file through a loop

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #948
    joepaul311
    Member

    I’m trying to generate a pdf called 1.pdf, 2.pdf, 3.pdf etc and it isn’t working. My code is:

    x <- 0
    for(i in 1:1000){
    x <- x + 1
    pdf(as.character(x),”.pdf”) #writes out to pdf <——- is this the issue?
    for(i in 1:1000000){
    hist(rnorm(1)) # graphs histogram, writen to the file
    }

    dev.off()
    }

    Also, I triedto just do
    a<- 1
    a
    and it wouldnt add a ot the file. help please

    #968
    bryan
    Participant

    Hi Joepaul,

    Yes, the pdf call is likely the problem. Try:

    pdf(paste(x, ".pdf", sep=''))

    Technically, you don’t need the extra counter (e.g., x) Just use your iterator variable/index:

    pdf(paste(i, ".pdf", sep=''))

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Scroll to top
Privacy Policy