[R] hist function in a for loop

Rui Barradas ruipbarradas at sapo.pt
Wed Jun 19 13:02:54 CEST 2013


Hello,

Your code doesn't work because you are calling a non-function:

hist(singlefile$CL1( ... )) # here

singlefile$CL1 is not a function.

For us to be able to help you please do the following.

1. paste the output of the command below in a post

dput(head(singlefile, 50))

2. Post a call to hist() that does work, without a for loop.

Rui Barradas


> I want to do the following :
>
> #I have created a huge csv.files with 44 colums
> #I want to select the specific colums from these files
> #CL1 consist data from which I want to compute the histogramms, CL2 is the
> cloumn which has numbers that identifies  know from which line my second
> histogram data should start.
> THE CSV FILE  loos like this:
>
> CL1   CL2  CL3   CL4     ..........    CLn
> 0.3    1        6.7      4.3          ...        ....
> ...       ..       ...           ....          ....        ....
> 0.8    2      ..         .....
>
> My target is to select only CL1 and CL2 compute histogram using CL1 data for
> each CL2-block as an example [1:2] until CL2 [1:60]
>
> I could print the histogramms but I can do only one by one. I want to
> compute all of them with the same binsize!!
>
> Therefore I wrote this code:
>
> #combine diffrent csv files into one
>
> files <- list.files (path = "./Inputfiles",".csv")
> numfiles <- length(files)
> print(files)
> singlefile <- list()
>
> #for loop
>          offset <- 1
>          mytotaldata <- list()   #mytotaldata includes merged csv.file
>          for (i in 1:numfiles)
> {
> 	mytotaldata[[files[i]]] <- read.csv(files[i], header = TRUE, sep = ","
> ,quote = "\"")
>
> #CL5 adding and giving an identification
>          mytotaldata[[files[i]]]["CL5"] <-  i
>
> #CL2 adding and create identification for the number of lines
>
> 	mytotaldata[[files[i]]]["CL2"] <-
> as.character(floor(as.numeric(rownames(mytotaldata[[files[i]]]))/1000)+offset)
> 	offset <- as.numeric(tail(mytotaldata[[files[i]]],1)["CL2"]) + 1
>
> #Create a singlefile for the whole data
>        	singlefile <- rbind(singlefile,mytotaldata[[files[i]]])
> }
>
> #Now I have combined csv file added 2 columns CL2, CL5
> # Compute the histograms
>      #library (lattice)
>      numfiles <- length(singlefile)  ###Is this necessary???????
>      for (i in 1:i)
>      {
>      #all the histograms with the same csv file
>      binsize <- -20 :20/2
>      hist(singlefile$CL1(singlefile$CL2[,1], freq = FALSE,xlab ="Graph i",
> col = "pink",main ="Example Histogram", ylim = c(-3.0,3.0)))
>      singlefile$GVCmin <- min(singlefile$CL1[1])
>      singlefile$GVCmin <- min(singlefile$CL1[1])
>      x1 <- seq(-3.0,3.0,by=.01)
>      lines(x1,dnorm(x1),col ="black")
>      }
>
> My struggle point is the for-loop with the histograms computation in the
> loop and using the binsize I have specified.
>
> Maybe now the question is clear!
> In case somebody has faced a similar problem ,please let me know about
> tircks, ideas !!
> I am trying many diffrent thing to let this for loop work but I did not find
> a solution, therefore I decided to ask in the forum
>
> Thanks in advance
> DZU
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/hist-function-in-a-for-loop-tp4669797p4669823.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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