1

I know that this is very easy in Phython, but I dont find any solutions in R for my problem:

I want to create multiple files like that:

write.table(df, "EUmax_20J_1993.dat", row.names=TRUE, col.names=TRUE) 
write.table(df, "EUmax_10J_1994.dat", row.names=TRUE, col.names=TRUE) 
write.table(df, "EUmax_20J_1996.dat", row.names=TRUE, col.names=TRUE) 
write.table(df, "EUmax_10J_1993.dat", row.names=TRUE, col.names=TRUE) 

In the beginning of my script I have this:

 Years = 10
 From = 1993

I use one script for all these outputs, I only change the amount of years and the startyear at the beginning of the script. Its running for the years I selected and then I want to adjust the output-name automatically. In Python you just combine characters with a simple plus.

In R I dont get it working, I tried this:

Years = as.character(Years)
From = as.character(From)
write.table(df, "EUmax_J_"+Years+"_"+From+".dat", row.names=TRUE, col.names=TRUE) 

It doesnt work and I dont find anything about how to connect characters like this.

Essi
  • 761
  • 3
  • 12
  • 22

1 Answers1

2

If you want filenames like this:

write.table(df, "EUmax_10J_1993.dat", row.names=TRUE, col.names=TRUE) 

to be created dynamically you use paste:

years <- 10
from <- 1993 

file_name <- paste(“EUmax_”, years, “J_”, from, “.dat”, col=“”, sep=“”)
write.table(df, file_name, row.names=TRUE, col.names=TRUE) 
Cybernetic
  • 12,628
  • 16
  • 93
  • 132