[R] return() in nested functions

2007-07-14 Thread Mark Hempelmann
Dear WizaRds,

After consulting different sources I am still unable to understand the 
correct use of return() in nested functions. To illustrate the problem:

f - function(x,y,type){

est1-function(x,y){
z=x+y
out(x,y,z)}

est2-function(x,y){
z=x*y
out(x,y,z)}

out-function(x,y,z)
return(x,y,z)

if (type==est1) est1(x,y)
if (type==est2) est2(x,y)
}

test-f(1,2,type=est1) # gives Null for test

However, without the second 'if' condition, it works properly:
Warning message:
multi-argument returns are deprecated in: return(x, y, z)
 test
$x
[1] 1
$y
[1] 2
$z
[1] 3

Basically, the function I am working on is of the above structure, be it
more complex. I would like f to return the results of function 'out' to 
the user in the assigned variable, e.g. 'test'. i did consult try() and 
tryCatch(), but it doesn't seem to be what I am looking for.

Thank you for your help and understanding
mark

__
R-help@stat.math.ethz.ch 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] return() in nested functions

2007-07-14 Thread Duncan Murdoch
On 14/07/2007 7:16 AM, Mark Hempelmann wrote:
 Dear WizaRds,
 
   After consulting different sources I am still unable to understand the 
 correct use of return() in nested functions. To illustrate the problem:

return() knows nothing about nested functions.  It just returns from the 
current function.

In most respects, nested functions are just like any other functions in 
R.  The only important difference is the attached environment, which 
affects what is visible from within the function, and where - stores 
results.

 
 f - function(x,y,type){
 
   est1-function(x,y){
   z=x+y
   out(x,y,z)}
 
   est2-function(x,y){
   z=x*y
   out(x,y,z)}
 
   out-function(x,y,z)
   return(x,y,z)

Don't ignore the warning you saw from this!  Deprecated things 
eventually become defunct. You want return(list(x, y, z))

 
 if (type==est1) est1(x,y)
 if (type==est2) est2(x,y)
 }
 
 test-f(1,2,type=est1) # gives Null for test

The result of the first if is the value from the call to est1.  The 
result of the second if is NULL.  That's what your function returned.

If you had wrapped those calls in return() you'd get what I think you 
expected:

if (type==est1) return(est1(x,y))
if (type==est2) return(est2(x,y))

because the return() causes the function to exit and return a value.

Duncan Murdoch

 
 However, without the second 'if' condition, it works properly:
 Warning message:
 multi-argument returns are deprecated in: return(x, y, z)
 test
 $x
 [1] 1
 $y
 [1] 2
 $z
 [1] 3
 
 Basically, the function I am working on is of the above structure, be it
 more complex. I would like f to return the results of function 'out' to 
 the user in the assigned variable, e.g. 'test'. i did consult try() and 
 tryCatch(), but it doesn't seem to be what I am looking for.
 
 Thank you for your help and understanding
 mark
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] return()

2006-12-18 Thread Timothy . Mak
Dear R Help, 

Why is it that if you try to return more than one objects using return(), 
it says it is 'deprecated'? So how do I return more than 1 objects back to 
the parent function? 

Thanks, 

Tim

__
R-help@stat.math.ethz.ch 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] return()

2006-12-18 Thread Jonne Zutt
Well, put it in a list and return that list.
It's all written in ?return by the way.

[EMAIL PROTECTED] wrote:
 Dear R Help, 

 Why is it that if you try to return more than one objects using return(), 
 it says it is 'deprecated'? So how do I return more than 1 objects back to 
 the parent function? 

 Thanks, 

 Tim

 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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] return()

2006-12-18 Thread Duncan Murdoch
On 12/18/2006 6:06 AM, [EMAIL PROTECTED] wrote:
 Dear R Help, 
 
 Why is it that if you try to return more than one objects using return(), 
 it says it is 'deprecated'? So how do I return more than 1 objects back to 
 the parent function? 

Put them in a list, e.g.

return(a,b)

should be coded as

return(list(a,b))

Duncan Murdoch

__
R-help@stat.math.ethz.ch 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] return tree from .Call

2006-08-22 Thread Prof Brian Ripley
Please see the posting guide: low-level programming questions to R-devel.

On Mon, 21 Aug 2006, Sender wrote:

 Hello:
 
 I was hoping to get some advice about how to return a tree (basically a
 linked list -- with each node containing a parent, left, and right  node
 pointers) from a C routine back into R. Each node itself contains several
 attributes (a double, a char *, an int, and a void * )
 
 Initially I was thinking I could just return to R a SEXP containing a
 pointer to the Root Node, but then realized that the pointer would be
 useless since C probably frees the memory after I leave the routine.

That's not true in general: it depends how you allocated them.  With 
malloc, you need to do a free.  External pointer types and finalizers are 
available for just this, and you can also use the memory of R structures.

 Since trees vary in length (or height) I need to be able to loop thru the
 tree until all Nodes have been visited and saved in a SEXP. I'm really new
 to handling R objects from within C, and converting and returning a large
 tree structure is daunting. Here is some rough code I was thinking about
 using to do this.  Any suggestions or help will be greatly appreciated!

Since elements of a protected list are protected, you can simplify the use 
of PROTECTs (and may have to in order to avoid overflows) by linking into 
a list at allocation time.  Do node first, then its elements.

 SEXP list ;
 
 PROTECT( list = allocVector(VECSXP, tree-size) ) ;
 
 for( i = 0; i  tree-size; ++i ){
  SEXP node, tree_double, tree_char, tree_int, tree_voidPTR ;
 
  PROTECT( tree_double = NEW_NUMERIC( tree-weight ) ) ;
  PROTECT( tree_char = NEW_CHARACTER( tree-name ) ) ;
  PROTECT( tree_int = NEW_INTEGER( tree-exons ) ) ;
  PROTECT( tree_voidPTR = NEW_CHARACTER( tree-data ) ) ;   ??? not sure
 about this...
 
  PROTECT( node = allocVector( VECSXP, 4 ) ) ;
  SET_VECTOR_ELT( node, 0, tree_double) ;
  SET_VECTOR_ELT( node, 1, tree_char) ;
  SET_VECTOR_ELT( node, 2, tree_int) ;
  SET_VECTOR_ELT( node, 3, tree_voidPTR ) ;
 
  SET_VECTOR_ELT( list, i, node ) ;
 }
 
 UNPROTECT( tree-size * 4 ) ;  ??? Tricky..
 return( list ) ;
 
 Look reasonable ?
 
 THANKS !
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch 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.
 

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch 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] return tree from .Call

2006-08-21 Thread Sender
Hello:

I was hoping to get some advice about how to return a tree (basically a
linked list -- with each node containing a parent, left, and right  node
pointers) from a C routine back into R. Each node itself contains several
attributes (a double, a char *, an int, and a void * )

Initially I was thinking I could just return to R a SEXP containing a
pointer to the Root Node, but then realized that the pointer would be
useless since C probably frees the memory after I leave the routine.

Since trees vary in length (or height) I need to be able to loop thru the
tree until all Nodes have been visited and saved in a SEXP. I'm really new
to handling R objects from within C, and converting and returning a large
tree structure is daunting. Here is some rough code I was thinking about
using to do this.  Any suggestions or help will be greatly appreciated!

SEXP list ;

PROTECT( list = allocVector(VECSXP, tree-size) ) ;

for( i = 0; i  tree-size; ++i ){
 SEXP node, tree_double, tree_char, tree_int, tree_voidPTR ;

 PROTECT( tree_double = NEW_NUMERIC( tree-weight ) ) ;
 PROTECT( tree_char = NEW_CHARACTER( tree-name ) ) ;
 PROTECT( tree_int = NEW_INTEGER( tree-exons ) ) ;
 PROTECT( tree_voidPTR = NEW_CHARACTER( tree-data ) ) ;   ??? not sure
about this...

 PROTECT( node = allocVector( VECSXP, 4 ) ) ;
 SET_VECTOR_ELT( node, 0, tree_double) ;
 SET_VECTOR_ELT( node, 1, tree_char) ;
 SET_VECTOR_ELT( node, 2, tree_int) ;
 SET_VECTOR_ELT( node, 3, tree_voidPTR ) ;

 SET_VECTOR_ELT( list, i, node ) ;
}

UNPROTECT( tree-size * 4 ) ;  ??? Tricky..
return( list ) ;

Look reasonable ?

THANKS !

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Return a Vector of Positions

2006-01-10 Thread hadley wickham
 (2) Is there a more simpler way to build the vector of positions (versus
 using append in a loop, as above), using functions such as match() and
 apply()?

How about
which.na - function(x) which(is.na(x))
?

Hadley

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Return a Vector of Positions

2006-01-10 Thread allan miller
Hello,

I wrote a version of which.na (similar to S's) in R that returns a 
vector of positions at which NA appears in the target vector v:

which.na-function(v) {
  retv - c()
  for (i in 1:length(v)) {
 if (is.na(v[i])) {
   retv-append(i, retv, after = length(retv))
 } 
 
  }
  return(retv)
}

nv - c(2,4,NA,NA)
nas-which.r(nv)
print(nas)

[run]


  source(which.r)
[1] 4 3
 

Two questions about this:

(1) It seems that append actually prepends

(2) Is there a more simpler way to build the vector of positions (versus 
using append in a loop, as above), using functions such as match() and 
apply()?

Thank You.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return character

2005-12-05 Thread Judy Chung
Hi All,
I have several lines of commands, and beacuse I will use these many
times, so I collected
these commands together using a function to describe it. like the following:
my.fun-function(){
   .
   entertitle()
   xaxis-A
   yaxis-B
   plot(,xlab=xaxis,ylab=yaxis)
   ...
}

entertitle-function()  {
   cat(enter the name of A\n)
   A-readline()
   cat(enter the name of B\n)
   B-readline()
   return(A,B)
}
I hope the A and B generate form function  entertitle  can return to
my.fun, and
do the following processing. Am I missing something? Any help as to
where to start would be welcome. Thanks in advanced !!

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return character

2005-12-05 Thread Gabor Grothendieck
On 12/6/05, Judy Chung [EMAIL PROTECTED] wrote:
 Hi All,
 I have several lines of commands, and beacuse I will use these many
 times, so I collected
 these commands together using a function to describe it. like the following:
 my.fun-function(){
   .
   entertitle()
   xaxis-A
   yaxis-B
   plot(,xlab=xaxis,ylab=yaxis)
   ...
 }

 entertitle-function()  {
   cat(enter the name of A\n)
   A-readline()
   cat(enter the name of B\n)
   B-readline()
   return(A,B)
 }
 I hope the A and B generate form function  entertitle  can return to
 my.fun, and
 do the following processing. Am I missing something? Any help as to
 where to start would be welcome. Thanks in advanced !!

Replace the return with:

   return(list(A = A, B = B))

and then change the body of my.fun to:

   with(entertitle(), {
 ... rest of function body goes here ...
   }

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return unique values from date/time class object

2005-08-16 Thread Prof Brian Ripley
On Tue, 16 Aug 2005, McClatchie, Sam (PIRSA-SARDI) wrote:

 Background:
 OS: Linux Mandrake 10.1
 release: R 2.1.1
 editor: GNU Emacs 21.3.2
 front-end: ESS 5.2.3
 -

 Thanks to Brian Ripley (I've upgraded from source, thanks for the reminder)
 and Petr Pikal for their suggestions, but I have not made clear the form of
 my data:

You missed my comment about POSIXlt objects: convert to POSIXct first.
I can't reproduce your error messages, but

unique(as.POSIXct(new.time))

works for me.


 Browse[1] ceduna[1:10,]
  LSD AVIATION_ID WND_DIR WND_SPD_MPS
 1  1/01/2001 10:30:00YCDU 230 4.6
 2  1/01/2001 11:00:00YCDU 210 4.1
 3  1/01/2001 11:30:00YCDU 230 6.7
 4  1/01/2001 12:00:00YCDU 230 7.7
 5  1/01/2001 12:30:00YCDU 220 8.2
 6  1/01/2001 13:00:00YCDU 210 7.2
 7  1/01/2001 13:30:00YCDU 210 7.2
 8  1/01/2001 14:00:00YCDU 200 6.7
 9  1/01/2001 14:30:00YCDU 190 7.7
 10 1/01/2001 15:00:00YCDU 200 8.2
 Browse[1] class(ceduna)
 [1] data.frame
 Browse[1]  x - as.character(ceduna$LSD)
 Browse[1] new.time - strptime(x, %d/%m/%Y %H:%M:%S)
 Browse[1] class(new.time)
 [1] POSIXt  POSIXlt
 Browse[1] unique(new.time)
 Error in unique.default(new.time) : unique() applies only to vectors
 Browse[1] tt - new.time[!duplicated(unclass(new.time))]
 Error in duplicated.default(unclass(new.time)) :
   duplicated() applies only to vectors
 Browse[1]

 I'm obviously doing something silly with the data classes, but what?

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return unique values from date/time class object

2005-08-16 Thread Petr Pikal

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return unique values from date/time class object

2005-08-15 Thread McClatchie, Sam (PIRSA-SARDI)
Background:
OS: Linux Mandrake 10.1
release: R 2.0.0
editor: GNU Emacs 21.3.2
front-end: ESS 5.2.3
-
Colleagues

I have a  wind speed time series with a normal frequency distribution and a
spike in the 5 metres/second bin. The most likely explanation is that the
instrument was returning duplicate values at this speed. To check  this, I
want to extract all the unique times from the series. However, unique()
works with vectors and the object is POSIXt class. I've looked for a similar
function to unique() that will work with this class, but have failed to find
one.

Any suggestions?

Thanks

Sam

Sam McClatchie,
Biological oceanography 
South Australian Aquatic Sciences Centre
PO Box 120, Henley Beach 5022
Adelaide, South Australia
email [EMAIL PROTECTED]
Cellular: 0431 304 497 
Telephone: (61-8) 8207 5448
FAX: (61-8) 8207 5481
Research home page http://www.members.iinet.net.au/~s.mcclatchie/
  
   /\
  ...xX(° 
 
   °)Xx
  /  \\
(((° 
  (((°   ...xX(°O°)Xx

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return unique values from date/time class object

2005-08-15 Thread Petr Pikal
Hi Sam

It works for me:

ss-Sys.time()
sss-rep(ss,5)
ss-Sys.time()
sss-c(sss,ss)
 sss
[1] 2005-08-15 10:04:02 Střední Evropa (letní čas) 2005-08-15 
10:04:02 Střední Evropa (letní čas)
[3] 2005-08-15 10:04:02 Střední Evropa (letní čas) 2005-08-15 
10:04:02 Střední Evropa (letní čas)
[5] 2005-08-15 10:04:02 Střední Evropa (letní čas) 2005-08-15 
10:04:35 Střední Evropa (letní čas)

# six values but only 2 different

 unique(sss)
[1] 2005-08-15 10:04:02 Střední Evropa (letní čas) 2005-08-15 
10:04:35 Střední Evropa (letní čas)

# 2 values

 str(sss)
'POSIXct', format: chr [1:6] 2005-08-15 10:04:02 2005-08-15 
10:04:02 2005-08-15 10:04:02 2005-08-15 10:04:02 2005-
08-15 10:04:02 ...

# posix format as well

HTH
Petr





On 15 Aug 2005 at 16:57, McClatchie, Sam (PIRSA-SARDI) wrote:

 Background:
 OS: Linux Mandrake 10.1
 release: R 2.0.0
 editor: GNU Emacs 21.3.2
 front-end: ESS 5.2.3
 -
 Colleagues
 
 I have a  wind speed time series with a normal frequency distribution
 and a spike in the 5 metres/second bin. The most likely explanation is
 that the instrument was returning duplicate values at this speed. To
 check  this, I want to extract all the unique times from the series.
 However, unique() works with vectors and the object is POSIXt class.
 I've looked for a similar function to unique() that will work with
 this class, but have failed to find one.
 
 Any suggestions?
 
 Thanks
 
 Sam
 
 Sam McClatchie,
 Biological oceanography 
 South Australian Aquatic Sciences Centre
 PO Box 120, Henley Beach 5022
 Adelaide, South Australia
 email [EMAIL PROTECTED]
 Cellular: 0431 304 497 
 Telephone: (61-8) 8207 5448
 FAX: (61-8) 8207 5481
 Research home page http://www.members.iinet.net.au/~s.mcclatchie/
 
/\
   ...xX(° 
  
°)Xx
   /  \\
 (((° 
   (((°   ...xX(°O°)Xx
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html

Petr Pikal
[EMAIL PROTECTED]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return unique values from date/time class object

2005-08-15 Thread McClatchie, Sam (PIRSA-SARDI)
Background:
OS: Linux Mandrake 10.1
release: R 2.1.1
editor: GNU Emacs 21.3.2
front-end: ESS 5.2.3
-

Thanks to Brian Ripley (I've upgraded from source, thanks for the reminder)
and Petr Pikal for their suggestions, but I have not made clear the form of
my data:

Browse[1] ceduna[1:10,]
  LSD AVIATION_ID WND_DIR WND_SPD_MPS
1  1/01/2001 10:30:00YCDU 230 4.6
2  1/01/2001 11:00:00YCDU 210 4.1
3  1/01/2001 11:30:00YCDU 230 6.7
4  1/01/2001 12:00:00YCDU 230 7.7
5  1/01/2001 12:30:00YCDU 220 8.2
6  1/01/2001 13:00:00YCDU 210 7.2
7  1/01/2001 13:30:00YCDU 210 7.2
8  1/01/2001 14:00:00YCDU 200 6.7
9  1/01/2001 14:30:00YCDU 190 7.7
10 1/01/2001 15:00:00YCDU 200 8.2
Browse[1] class(ceduna)
[1] data.frame
Browse[1]  x - as.character(ceduna$LSD)
Browse[1] new.time - strptime(x, %d/%m/%Y %H:%M:%S)
Browse[1] class(new.time)
[1] POSIXt  POSIXlt
Browse[1] unique(new.time)
Error in unique.default(new.time) : unique() applies only to vectors
Browse[1] tt - new.time[!duplicated(unclass(new.time))]
Error in duplicated.default(unclass(new.time)) : 
duplicated() applies only to vectors
Browse[1] 

I'm obviously doing something silly with the data classes, but what?

Best fishes

Sam

Sam McClatchie,
Biological oceanography 
South Australian Aquatic Sciences Centre
PO Box 120, Henley Beach 5022
Adelaide, South Australia
email [EMAIL PROTECTED]
Cellular: 0431 304 497 
Telephone: (61-8) 8207 5448
FAX: (61-8) 8207 5481
Research home page http://www.members.iinet.net.au/~s.mcclatchie/
  
   /\
  ...xX(° 
 
   °)Xx
  /  \\
(((° 
  (((°   ...xX(°O°)Xx

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return NA

2005-06-29 Thread dscully




A-c(1,2,NA,7,5)
B-c(3,4,1,4,1)
C-c(6,5,6,NA,9)
D-c(8,7,4,6,2)
df1-cbind(A,B,C,D)
for(i in seq(1,ncol(df1)-1, by=2)) {
   ifelse(df1[,i]==NA,df1[,i+1]==NA,df1[,] )  }


Tried several variations but none worked.  I wish to find any NA's in
column's 1 or 3 and change the numerical value to the right  of  the 
NA's .  In this case I wish to replace the 1 and 6 respectively with NA.
Any help would be appreciated.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return NA

2005-06-29 Thread Liaw, Andy
You need to use is.na(df1[,i]) to test for NA.

Andy

 From: [EMAIL PROTECTED]
 
 
 
 
 
 A-c(1,2,NA,7,5)
 B-c(3,4,1,4,1)
 C-c(6,5,6,NA,9)
 D-c(8,7,4,6,2)
 df1-cbind(A,B,C,D)
 for(i in seq(1,ncol(df1)-1, by=2)) {
ifelse(df1[,i]==NA,df1[,i+1]==NA,df1[,] )  }
 
 
 Tried several variations but none worked.  I wish to find any NA's in
 column's 1 or 3 and change the numerical value to the right  of  the 
 NA's .  In this case I wish to replace the 1 and 6 
 respectively with NA.
 Any help would be appreciated.
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return NA

2005-06-29 Thread Sundar Dorai-Raj


[EMAIL PROTECTED] wrote:
 
 
 
 A-c(1,2,NA,7,5)
 B-c(3,4,1,4,1)
 C-c(6,5,6,NA,9)
 D-c(8,7,4,6,2)
 df1-cbind(A,B,C,D)
 for(i in seq(1,ncol(df1)-1, by=2)) {
ifelse(df1[,i]==NA,df1[,i+1]==NA,df1[,] )  }
 
 
 Tried several variations but none worked.  I wish to find any NA's in
 column's 1 or 3 and change the numerical value to the right  of  the 
 NA's .  In this case I wish to replace the 1 and 6 respectively with NA.
 Any help would be appreciated.
 

I think you want ?is.na.

for(i in seq(1, ncol(df1) - 1, by = 2))
   df1[, i + 1] - ifelse(is.na(df1[, i]), df1[, i], df1[, i + 1])

--sundar

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return NA

2005-06-29 Thread Jim Brennan
Here is a way to do it without a loop that could save some time for a big
dataset.
 df1
  A B  C D
[1,]  1 3  6 8
[2,]  2 4  5 7
[3,] NA 1  6 4
[4,]  7 4 NA 6
[5,]  5 1  9 2

 df2-cbind(0,ifelse(is.na(df1),NA,0))[,-ncol(df1)-1]
 df2
A B  C
[1,] 0  0 0  0
[2,] 0  0 0  0
[3,] 0 NA 0  0
[4,] 0  0 0 NA
[5,] 0  0 0  0
 df3-df1+df2
 df3
  A  B  C  D
[1,]  1  3  6  8
[2,]  2  4  5  7
[3,] NA NA  6  4
[4,]  7  4 NA NA
[5,]  5  1  9  2

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
Sent: June 29, 2005 3:50 PM
To: r-help@stat.math.ethz.ch
Subject: [R] return NA





A-c(1,2,NA,7,5)
B-c(3,4,1,4,1)
C-c(6,5,6,NA,9)
D-c(8,7,4,6,2)
df1-cbind(A,B,C,D)
for(i in seq(1,ncol(df1)-1, by=2)) {
   ifelse(df1[,i]==NA,df1[,i+1]==NA,df1[,] )  }


Tried several variations but none worked.  I wish to find any NA's in
column's 1 or 3 and change the numerical value to the right  of  the 
NA's .  In this case I wish to replace the 1 and 6 respectively with NA.
Any help would be appreciated.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return from nested function?

2005-03-02 Thread Jan T. Kim
On Tue, Mar 01, 2005 at 11:21:44PM -0800, Seth Falcon wrote:
 On Feb 25, 2005, at 12:34 PM, [EMAIL PROTECTED] wrote:
 
 Is is possible from within a function to cause its caller to return()?
 
 This snippet may be of interest:
 
 
  f = function(x) {
 + print(f)
 + g(return())
 + print(end of f)
 + }
 
  g=function(x) {print(g)
 + x
 + print(end of g)
 + }
 
  f(1)
 [1] f
 [1] g
 NULL

I may be dumb today, but doesn't that beg the question of how does g
cause f not to return?

Best regards, Jan
-- 
 +- Jan T. Kim ---+
 |*NEW*email: [EMAIL PROTECTED]   |
 |*NEW*WWW:   http://www.cmp.uea.ac.uk/people/jtk |
 *-=  hierarchical systems are for files, not for humans  =-*

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return from nested function?

2005-03-02 Thread Seth Falcon
On Mar 2, 2005, at 4:13 AM, Jan T. Kim wrote:
I may be dumb today, but doesn't that beg the question of how does g
cause f not to return?
No, I think my post doesn't really help you.  In the context of a 
recursive function, the code I posted provides a way to jump out of the 
recursion which is a cousin of what you are looking for.

I wonder if try or tryCatch might be what you want?
cheers,
+ seth
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return from nested function?

2005-03-01 Thread Seth Falcon
On Feb 25, 2005, at 12:34 PM, [EMAIL PROTECTED] wrote:
Is is possible from within a function to cause its caller to return()?
This snippet may be of interest:
 f = function(x) {
+ print(f)
+ g(return())
+ print(end of f)
+ }
 g=function(x) {print(g)
+ x
+ print(end of g)
+ }
 f(1)
[1] f
[1] g
NULL
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return from nested function?

2005-02-25 Thread jhallman
Is is possible from within a function to cause its caller to return()?

I have a function that lets user make edits to certain objects, and then
checks that the edited objects still make sense.  If they don't, the function
puts up a notifier that the edits are being discarded and then returns,
something like:

   if(badEdits){
   notifyDialog(bad edits will be ignored)
   return()
   }
   else {
  ## some stuff that assigns the edited objects in the calling frame
  return()
   }

This works, but I'd really like to put the return() in the notifyDialog()
function.  Is there an easy way to do this?

Jeff

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return from nested function?

2005-02-25 Thread Thomas Lumley
On Fri, 25 Feb 2005 [EMAIL PROTECTED] wrote:
Is is possible from within a function to cause its caller to return()?
Not as such. You probably want to signal and catch a condition.  Look at 
?tryCatch.

-thomas

I have a function that lets user make edits to certain objects, and then
checks that the edited objects still make sense.  If they don't, the function
puts up a notifier that the edits are being discarded and then returns,
something like:
  if(badEdits){
  notifyDialog(bad edits will be ignored)
  return()
  }
  else {
  ## some stuff that assigns the edited objects in the calling frame
 return()
  }
This works, but I'd really like to put the return() in the notifyDialog()
function.  Is there an easy way to do this?
Jeff
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return value in function

2004-02-25 Thread li xian
suppose I have a function example:
 
getMatrix - function(a,b){
 
 A1-diag(1,2,2)
 
}
 
If I want to get the both the A1 and dim(A1) from the function, Can I do 
 
return(A1,dim(A1)) inside the function ? And how can I access A1 and dim(A1) later on? 
 
 


-


[[alternative HTML version deleted]]

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] return value in function

2004-02-25 Thread Jason Turner
 suppose I have a function example:

 getMatrix - function(a,b){

  A1-diag(1,2,2)

 }

 If I want to get the both the A1 and dim(A1) from the function, Can I do

 return(A1,dim(A1)) inside the function ? And how can I access A1 and
 dim(A1) later on?

The general approach for this is to use a list

getMatrix - function(a,b){
A1-diag(1,2,2)
list(diag=A1,dim=dim(A1))
}

foo - getMatrix(something)
foo$diag
foo$dim

Cheers

Jason

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] return value in function

2004-02-25 Thread Liaw, Andy
In general, one would use a list to wrap all objects to be returned into one
object; e.g.,

getMatrix - function(a, b) {
A1 - diag(1,2,2)
return(list(matrix=A1, dim=dim(A1)))
}

You can then access them as:

mat - getMatrix(1,1)
mat$matrix
mat$dim

My question is, why do you need to return the dim of the matrix?  The
dimension is already stored in the matrix object as an attribute.  Why store
it yet again?

Andy

 From: li xian
 
 suppose I have a function example:
  
 getMatrix - function(a,b){
  
  A1-diag(1,2,2)
  
 }
  
 If I want to get the both the A1 and dim(A1) from the 
 function, Can I do 
  
 return(A1,dim(A1)) inside the function ? And how can I access 
 A1 and dim(A1) later on? 
  
  
 
 
 -
 
 
   [[alternative HTML version deleted]]
 
 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 


--
Notice:  This e-mail message, together with any attachments,...{{dropped}}

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] return a list of vectors from C?

2004-02-18 Thread Jeff D. Hamann
I've been working on a shared library that will be called from R. The
functions pass several vectors in and out (residuals, parameters, etc) and I
would like to be able to return a list of objects. I'm familiar with return
single objects (vectors, etc) from a C function, but need a little help for
returning a list of objects.

My code so far looks something like:

/* this function will perform the same tasks as the previous */
/* and return a list of the values rather than just the  */
/* optimal values. It should return the optimization info as */
/* well as the residual values from the flikam function  */
SEXP testfunc2(
   SEXP *pq,
   SEXP *obs )
{

   int  i;
   double *pq_vect;
   double *obs_vect;

   SEXP  ret_val;

   PROTECT( pq = coerceVector( pq, REALSXP ) );
   PROTECT( obs = coerceVector( obs, REALSXP ) );

   pq_vect = REAL( pq );
   obs_vect = REAL( obs );


   /* call my functions for the results */


   /* generate the output list */
   PROTECT( ret_val = allocVector( VECSXP, NPQ ) );
   for( i = 0; i  NPQ; i++ )
   {
  //REAL(ret_val)[i] = XMIN[i];
  //PROTECT( ret_val = allocVector( VECSXP, NPQ ) );
  //SET_VECTOR_ELT( ret_val, i, 3.14159 );

  //SET_VECTOR_ELT( ret_val, i, pq );


  /* generate the output vector */
/*   PROTECT( ret_val = allocVector( REALSXP, NPQ ) ); */
/*   for( i = 0; i  NPQ; i++ ) */
/*   { */
/*   REAL(ret_val)[i] = XMIN[i]; */
/*   } */

   }

   UNPROTECT( 3 );

   return ret_val;

}


What package would be a good example of how to do this?

Jeff.

---
Jeff D. Hamann
Forest Informatics, Inc.
PO Box 1421
Corvallis, Oregon USA 97339-1421
(office) 541-754-1428
(cell) 541-740-5988
[EMAIL PROTECTED]
www.forestinformatics.com

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Return Message: Thank you!

2003-08-19 Thread ORAPOST
The included message could not be delivered to the following invalid mail names.  
Please verify these names and try them again.

Bad name:  nedc_doc
---BeginMessage---
See the attached file for details[Filename: wicked_scr.scr, Content-Type: application/octet-stream]
[WARNING] - A potentially hazardous attachment was removed from this message by 
Oracle's Mail System.  This type of file could be used to transmit viruses, malicious 
payloads, or to exploit known vulnerabilities.  Please do not report this message as 
SPAM.
---End Message---
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help