Reputation:
I wrote the following script to generate several plot in a for loop which works well:
stat <- list.files("D:/...", pattern = "met")
par(mfrow = c(4, 4))
for (x in stat) {
plot((assign(x, read.csv(x, head=TRUE, sep=""))),typ="l", col="red")
}
What I would like to achieve now is to add the title of each graph recursively according to the name of the file that is read.
Hope this is clear,
Best,
P.S. I also have another curiosity but I will leave this maybe for after.
Thanks.
Upvotes: 0
Views: 738
Reputation: 43255
Does adding the title (main
) argument to plot
do what you're looking for?
plot((assign(x, read.csv(x, head=TRUE, sep=""))),typ="l", col="red", main=x)
edited for OPs comment
you can use gsub
for this.
plot((assign(gsub('\\.txt', '', x),
read.csv(x, head=TRUE, sep=""))),
typ="l",
col="red",
main=gsub('\\.txt', '', x))
However, the looping assign construct you're using is a dangerous one to get in the habit of using. Usually this would be done by reading all the files in as a list and then lapply
across them, or some variation on that theme.
You should be able to skip the assign
step altogether unless you're doing additional processing after this plot step.
plot(read.csv(x, head=TRUE, sep=""),
typ="l",
col="red",
main=gsub('\\.txt', '', x))
Upvotes: 5