[R] A simple For-Loop doesn't work
Berend Hasselman
bhh at xs4all.nl
Sun Mar 15 09:45:41 CET 2015
> On 14-03-2015, at 18:28, Nicolae Doban <nickdoban at gmail.com> wrote:
>
> Hello,
>
> my name is Nick and I'm working on a project. I'm having trouble with
> building a simple for-loop. In this loop I want to read csv files, perform
> a corr function and save it to a pdf file. I tried to solve this problem by
> looking for solutions online but couldn't figure it out. Could you also
> tell me if if it is possible to name the dataframe(grid.table())?Could you
> please help me?
>
> The code I wrote and which doesn't work is:
> * <Code>*
> Data <- c("July", "August", "September")
>
> pdf("Cor.pdf")
> setwd("path")
> for(i in 1:6){
>
> Data[i] <- read.csv(Data[i],".csv", header=T)
>
> grid.table(cor(Data[i][3:10]))
> corrgram(Data[i], order=TRUE, lower.panel=panel.shade,
> upper.panel=panel.pie, text.panel=panel.txt,
> main=Data[i],"Cor")
>
> }
> dev.off()
> * </Code>*
>
You did not tell us what error messages you are getting or what is going wrong.
In addition to the previous remark: you are not creating the file name in read.csv in the correct way.
The filename is a single string. Create it with e.g. paste0(Data[i],”csv”) or possibly if required with file.path(…).
Furthermore you are overwriting Data[i] with the result of read.csv. Why?
Use something like DT <- read.csv(….) and change the remainder of your commands to use DT.
Please do not use the abbreviation T for TRUE.
> Thank you,
> Nick
>
> [[alternative HTML version deleted]]
>
Please do not post in html as the Posting guide asks.
Berend
More information about the R-help
mailing list