hist function in a for loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Dzu
Reply | Threaded
Open this post in threaded view
|

hist function in a for loop

Dzu
Dear all,

I need to create a for-loop in which I can compute multiple histograms
My code is the following :
#singlefile includes huge csv file
#I want to specify the binsize
#I would like to compute in the for -loop the histograms


    numfiles <- length(singlefile)
    for (i in 1 :51)
    {
    binsize <- -20 :20/2
    hist(singlefile(singlefile$GVC[singlefile$new_id==i]], break=seq(), by = binsize)))

What do I have to do ?
How can I specify the range for i  ?

I am totally lost
Thanks for support
D.U
Ms.Dizem Uerek
Reply | Threaded
Open this post in threaded view
|

Re: hist function in a for loop

Rui Barradas
Hello,

Sorry, I forgot to Cc the list.

Rui Barradas
Em 18-06-2013 16:29, Rui Barradas escreveu:

> Hello,
>
> Inline.
>
> Em 18-06-2013 15:54, Dzu escreveu:
>> Dear all,
>>
>> I need to create a for-loop in which I can compute multiple histograms
>> My code is the following :
>> #singlefile includes huge csv file
>> #I want to specify the binsize
>> #I would like to compute in the for -loop the histograms
>>
>>
>>      numfiles <- length(singlefile)
>>      for (i in 1 :51)
>>      {
>>      binsize <- -20 :20/2
>>      hist(singlefile(singlefile$GVC[singlefile$new_id==i]],
>> break=seq(), by =
>> binsize)))
>
> hist() does not have a 'by' argument.
> Maybe what you want is something like the following.
>
> binsize <- -20:20/2
> h <- vector("list", 51)
> for (i in 1:51)
>      h[[i]] <- hist(singlefile$GVC[singlefile$new_id==i, ],
> break=binsize, plot = FALSE)
>
>
> This computes the histograms. Now to plot the histograms you use the
> plot() function:
>
> plot(h[[1]])  # plots the first
>
>
>>
>> What do I have to do ?
>> How can I specify the range for i  ?
>
> I don't understand. You want to match several values for i at once?
>
> singlefile$GVC[singlefile$new_id %in% irange, ]
>
>
> Hope this helps,
>
> Rui Barradas
>>
>> I am totally lost
>> Thanks for support
>> D.U
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/hist-function-in-a-for-loop-tp4669797.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] 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.
>>

______________________________________________
[hidden email] 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.
Dzu
Reply | Threaded
Open this post in threaded view
|

Re: hist function in a for loop

Dzu
Hello
Thanks for reply

I want to compute several histograms in a for loop.I am trying to set the binsize constant in the beginning.

#compute the histograms
 for (i in 1:12)
    {
    binsize <- -20 :20/2

    hist(singlefile$GVC(singlefile$new_id[,i], freq = FALSE,xlab ="Graph i", col = "pink",main ="Example Histogram", ylim = c(-3.0,3.0)))
    singlefile$GVCmin <- min(singlefile$GVC[1])
    singlefile$GVCmin <- min(singlefile$GVC[1])
    x1 <- seq(-3.0,3.0,by=.01)
    lines(x1,dnorm(x1),col ="black")
    }

I tried also this but it does not do anything. I also tried your proposal , but it says that : breaks = binsize  is not allowed.

I think I am totaly far away from that what I want to do with my code .

One single histogram plotting and computing is easy , but if it is in the loop , by the syntax to feed the function with counter i is not working

Thanks
Dizem
Ms.Dizem Uerek
Dzu
Reply | Threaded
Open this post in threaded view
|

Re: hist function in a for loop

Dzu
Dear,

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











Ms.Dizem Uerek
Reply | Threaded
Open this post in threaded view
|

Re: hist function in a for loop

Rui Barradas
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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.