Re: [R] speeding up loop and dealing wtih memory problems

From: David Hajage <dhajage.r_at_gmail.com>
Date: Mon, 28 Jul 2008 15:40:26 +0200

Is it what you want ?

> x <- matrix(c(1:3, NA, NA, 4, 1:2, NA), 3, 3)
> x

     [,1] [,2] [,3]

[1,]    1   NA    1
[2,]    2   NA    2
[3,]    3    4   NA

> x[is.na(x)] <- 0
> x

     [,1] [,2] [,3]

[1,]    1    0    1
[2,]    2    0    2
[3,]    3    4    0


2008/7/28 Denise Xifara <dionysia-kiara.xifaras_at_st-hildas.ox.ac.uk>

> Dear All and Mark,
>
> Given a dataset that I have called dat, I was hoping to speed up the
> following loop:
>
> for(i in 1:835353){
> for(j in 1:86){
> if (is.na(dat[i,j])==TRUE){dat[i,j]<-0 }}}
> Actually I am also having a memory problem. I get the following:
>
> Error: cannot allocate vector of size 3.2 Mb
> In addition: Warning messages:
> 1: In dat[i, j] <- 0 :
> Reached total allocation of 1535Mb: see help(memory.size)
> 2: In dat[i, j] <- 0 :
> Reached total allocation of 1535Mb: see help(memory.size)
> 3: In dat[i, j] <- 0 :
> Reached total allocation of 1535Mb: see help(memory.size)
> 4: In dat[i, j] <- 0 :
> Reached total allocation of 1535Mb: see help(memory.size)
>
> If I try and apply the loop just to a particular column, rather than the
> whole dataset, so that I dont have the memory problem, ie
>
> for(i in 1:835353){
> if (is.na(dat[i,4])==TRUE){dat[i,4]<-0 }}
>
> it takes ridiculously long to process, so I was hoping that there would be
> a
> quicker way to do this.
>
> Thank you all very much for the help,
> Denise
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Mon 28 Jul 2008 - 13:46:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jul 2008 - 14:32:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive