[R] Check for is.object

2010-11-22 Thread Santosh Srinivas
Hello,

I am trying to recursively append some data from multiple files into a
common object

For this, I am using in a loop

NewObject - rbind(NewObject,tempObject)


For the first loop, obviously there is no NewObject ... so I wanted to do
NewObject - tempObject[0,]

Now when it loops again I want to put the statement do NewObject -
tempObject[0,] inside a if statement ... so that it does I can skip it once
NewObject has been initialized.

But, is.object doesn't seem to work. 

What is the alternative check that I can do? And is there a better way to
achieve what I want?

Thanks,
S

__
R-help@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.


Re: [R] Check for is.object

2010-11-22 Thread Jonathan P Daily
I think you want the function ?exists

if(!exists(NewObject))

--
Jonathan P. Daily
Technician - USGS Leetown Science Center
11649 Leetown Road
Kearneysville WV, 25430
(304) 724-4480
Is the room still a room when its empty? Does the room,
 the thing itself have purpose? Or do we, what's the word... imbue it.
 - Jubal Early, Firefly

r-help-boun...@r-project.org wrote on 11/22/2010 10:14:51 AM:

 [image removed] 
 
 [R] Check for is.object
 
 Santosh Srinivas 
 
 to:
 
 r-help
 
 11/22/2010 10:17 AM
 
 Sent by:
 
 r-help-boun...@r-project.org
 
 Hello,
 
 I am trying to recursively append some data from multiple files into a
 common object
 
 For this, I am using in a loop
 
 NewObject - rbind(NewObject,tempObject)
 
 
 For the first loop, obviously there is no NewObject ... so I wanted to 
do
 NewObject - tempObject[0,]
 
 Now when it loops again I want to put the statement do NewObject -
 tempObject[0,] inside a if statement ... so that it does I can skip it 
once
 NewObject has been initialized.
 
 But, is.object doesn't seem to work. 
 
 What is the alternative check that I can do? And is there a better way 
to
 achieve what I want?
 
 Thanks,
 S
 
 __
 R-help@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.

__
R-help@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.


Re: [R] Check for is.object

2010-11-22 Thread Phil Spector

Santosh -
   The simple answer to your question is to initialize 
NewObject to NULL before the loop, i.e.


   newObject = NULL

However, I should point out this is one of the most 
inefficient ways to program in R.  A far better way is

to allocate enough space for NewObject outside your
loop, and fill it in in the loop.  Here's a simple
example to give you an idea of the difference in time
the two methods require:


system.time({answer = matrix(NA,1000,5);

+  for(i in 1:1000)answer[i,] - sample(10,5)})
   user  system elapsed
  0.020   0.000   0.017 

system.time({answer=NULL;

+  for(i in 1:1000)answer=rbind(answer,sample(10,5))})
   user  system elapsed
  0.072   0.000   0.070

However, it gets even worse if the sample size is larger:


system.time({answer = matrix(NA,1,5);

+  for(i in 1:1)answer[i,] - sample(10,5)})
   user  system elapsed
  0.184   0.000   0.184 

system.time({answer=NULL;for(i in 1:1)

+  answer=rbind(answer,sample(10,5))})
   user  system elapsed
  5.492   0.032   5.562

Even if you don't know how big your newObject matrix will
become, it's still far more efficient to overallocate the 
matrix and then truncate it at the end.


I'd strongly recommend that you avoid building your matrix
incrementally inside a loop!

- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu


On Mon, 22 Nov 2010, Santosh Srinivas wrote:


Hello,

I am trying to recursively append some data from multiple files into a
common object

For this, I am using in a loop

NewObject - rbind(NewObject,tempObject)


For the first loop, obviously there is no NewObject ... so I wanted to do
NewObject - tempObject[0,]

Now when it loops again I want to put the statement do NewObject -
tempObject[0,] inside a if statement ... so that it does I can skip it once
NewObject has been initialized.

But, is.object doesn't seem to work.

What is the alternative check that I can do? And is there a better way to
achieve what I want?

Thanks,
S

__
R-help@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.



__
R-help@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.