2

I want to save 5 files in the same folder but I am not able to change the file name at each iteration.

for
i=1:5
{
a=data.frame(weibull_1=rweibull(10000,2.5,10000), weibull_2=rweibull(10000,3,5000),normal=rnorm(10000,0,0.03))
write.csv(a,file="i.csv")
}

3 Answers 3

2

Use paste0 to create the title for your file. Also, your for-loop constructor is a bit off:

for (i in 1:5)
{
  a=data.frame(weibull_1=rweibull(10000,2.5,10000), weibull_2=rweibull(10000,3,5000),normal=rnorm(10000,0,0.03))
  write.csv(a,file=paste0(i,".csv"))
}
Sign up to request clarification or add additional context in comments.

Comments

1

We can use lapply

lapply(1:5, function(i) {
   a=data.frame(weibull_1=rweibull(10000,2.5,10000), 
      weibull_2=rweibull(10000,3,5000),normal=rnorm(10000,0,0.03)) 
   write.csv(a,file=paste0(i,".csv"))
  })

Comments

1

Use either paste or paste0 in place of filename in write.csv. paste0 is similar to paste with just a fixed seperator e.g

paste0(a,b) = paste(a,b,sep="")

So write.csv should be changed as either

Write.csv(a,file=paste0(i,".csv"));

Or you can use

write.csv(a,file=paste(i,".csv",sep=""));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.