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]]
Pay attention to the i counter in the loop: it runs from 1 to 6 but data has only 3 elements Il 15/mar/2015 04:56 "Nicolae Doban" <nickdoban at gmail.com> ha scritto:> 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. >[[alternative HTML version deleted]]
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. >
> 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