[R] A simple For-Loop doesn't work

Rainer Schuermann rainer.schuermann at gmx.net
Sun Mar 15 09:40:44 CET 2015


Hi Nick,

Your code is not exactly "commented, minimal, self-contained, reproducible" and contains a number of inconsistencies (Data has three elements, your loop expects six). Try something like

Data <- c("July", "August",  "September")
...
for( x in Data )
{
	currentData <- read.csv( paste( x, ".csv", sep = "" ), header = TRUE )
	...
}

to get your loop going.

Rgds,
Rainer



On Saturday 14 March 2015 18:28:28 Nicolae Doban 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>*
> 
> Thank you,
> Nick
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list