writing function

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

writing function

arnaud Gaboury
Dear group,

I am trying to write functions, but as a beginner, everything is not so
obvious.
Let's say I want the results in a list of elemts like this :
tot1, tot2, etc

Here is a function:

toto <-
function(x,y)

{

for(i in x:y){

paste(c("tot",i),collapse="")<-(i*2)

}
}

If I type this :
>toto(1,5)
I get this message error:
Error in paste(c("tot", i), collapse = "") <- (i * 2) :
  target of assignment expands to non-language object

How can I write it to get the result I want (i.e tot1, tot2... with tot1=2,
tot2=4...) in my environment?

TY for any help

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

Re: writing function

Tal Galili
Try this:

paste("tot", 4:16, sep = "")


Or:

func <- function(x,y)
{
 paste("tot", x:y, sep = "")
}
func(4,16)




----------------Contact
Details:-------------------------------------------------------
Contact me: [hidden email] |  972-52-7275845
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------




On Thu, May 20, 2010 at 6:54 PM, arnaud Gaboury <[hidden email]>wrote:

> Dear group,
>
> I am trying to write functions, but as a beginner, everything is not so
> obvious.
> Let's say I want the results in a list of elemts like this :
> tot1, tot2, etc
>
> Here is a function:
>
> toto <-
> function(x,y)
>
> {
>
> for(i in x:y){
>
> paste(c("tot",i),collapse="")<-(i*2)
>
> }
> }
>
> If I type this :
> >toto(1,5)
> I get this message error:
> Error in paste(c("tot", i), collapse = "") <- (i * 2) :
>  target of assignment expands to non-language object
>
> How can I write it to get the result I want (i.e tot1, tot2... with tot1=2,
> tot2=4...) in my environment?
>
> TY for any help
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

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

Re: writing function

Ivan Calandra
In reply to this post by arnaud Gaboury
Hi,

the problem is not about the environment, but about the
     paste(c("tot", i), collapse = "")
which is not recognized as an object.
Maybe assign() could do the trick

You could also do it this way (though it's not exactly what you want,
but it might be better):
toto <- function(x,y){
   tot <- vector(mode="list", length=(y-x+1))   ##create an empty list
of correct length
   for (i in x:y) {
     tot[[i]] <- (i*2)   ##store each value into each element of the list
   }
   return(tot)  ##return the list
}

HTH,
Ivan




Le 5/20/2010 17:54, arnaud Gaboury a écrit :

> Dear group,
>
> I am trying to write functions, but as a beginner, everything is not so
> obvious.
> Let's say I want the results in a list of elemts like this :
> tot1, tot2, etc
>
> Here is a function:
>
> toto<-
> function(x,y)
>
> {
>
> for(i in x:y){
>
> paste(c("tot",i),collapse="")<-(i*2)
>
> }
> }
>
> If I type this :
>    
>> toto(1,5)
>>      
> I get this message error:
> Error in paste(c("tot", i), collapse = "")<- (i * 2) :
>    target of assignment expands to non-language object
>
> How can I write it to get the result I want (i.e tot1, tot2... with tot1=2,
> tot2=4...) in my environment?
>
> TY for any help
>
> ______________________________________________
> [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.
>
>    

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

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

Re: writing function

Ivan Calandra
Hi Tal,

You're probably right, but I think it's never a waste to get diverse solutions :)

Cheers,
Ivan


Le 20 mai 2010 à 19:26, Tal Galili a écrit :

> Hi Ivan,
>
> I am not sure if the poster (arnaud) intended for a list() object (although that's what he wrote, I suspect he intended for a vector - hence my proposed solution).
>
> Cheers,
> Tal
>
>
> ----------------Contact Details:-------------------------------------------------------
> Contact me: [hidden email] |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
>
>
>
> On Thu, May 20, 2010 at 7:20 PM, Ivan Calandra <[hidden email]> wrote:
> Hi,
>
> the problem is not about the environment, but about the
>
>    paste(c("tot", i), collapse = "")
> which is not recognized as an object.
> Maybe assign() could do the trick
>
> You could also do it this way (though it's not exactly what you want, but it might be better):
> toto <- function(x,y){
>  tot <- vector(mode="list", length=(y-x+1))   ##create an empty list of correct length
>
>  for (i in x:y) {
>    tot[[i]] <- (i*2)   ##store each value into each element of the list
>  }
>  return(tot)  ##return the list
> }
>
> HTH,
> Ivan
>
>
>
>
> Le 5/20/2010 17:54, arnaud Gaboury a écrit :
>
> Dear group,
>
> I am trying to write functions, but as a beginner, everything is not so
> obvious.
> Let's say I want the results in a list of elemts like this :
> tot1, tot2, etc
>
> Here is a function:
>
> toto<-
> function(x,y)
>
> {
>
> for(i in x:y){
>
> paste(c("tot",i),collapse="")<-(i*2)
>
> }
> }
>
> If I type this :
>  
> toto(1,5)
>    
> I get this message error:
> Error in paste(c("tot", i), collapse = "")<- (i * 2) :
>   target of assignment expands to non-language object
>
> How can I write it to get the result I want (i.e tot1, tot2... with tot1=2,
> tot2=4...) in my environment?
>
> TY for any help
>
> ______________________________________________
> [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.
>
>  
>
> --
> Ivan CALANDRA
> PhD Student
> University of Hamburg
> Biozentrum Grindel und Zoologisches Museum
> Abt. Säugetiere
> Martin-Luther-King-Platz 3
> D-20146 Hamburg, GERMANY
> +49(0)40 42838 6231
> [hidden email]
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
>
> ______________________________________________
> [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.
>


--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Institut und Museum
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php






        [[alternative HTML version deleted]]


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

Re: writing function

Joshua Wiley-2
In reply to this post by arnaud Gaboury
Hello,

I am guessing by my environment you mean the global environment (where
you normally assign things from the console).  It also looks like you
would like the results of your function call to be a set of new
objects created.  If that is what you are looking for, try:

toto <- function(x,y) {
for(i in x:y){
  assign(paste(c("tot",i),collapse=""), (i*2), envir=.GlobalEnv)
       }
}

toto(1,5) #creates 5 objects in the global environment, tot1, tot2, ..., tot5

HTH,

Josh

On Thu, May 20, 2010 at 8:54 AM, arnaud Gaboury
<[hidden email]> wrote:

> Dear group,
>
> I am trying to write functions, but as a beginner, everything is not so
> obvious.
> Let's say I want the results in a list of elemts like this :
> tot1, tot2, etc
>
> Here is a function:
>
> toto <-
> function(x,y)
>
> {
>
> for(i in x:y){
>
> paste(c("tot",i),collapse="")<-(i*2)
>
> }
> }
>
> If I type this :
>>toto(1,5)
> I get this message error:
> Error in paste(c("tot", i), collapse = "") <- (i * 2) :
>  target of assignment expands to non-language object
>
> How can I write it to get the result I want (i.e tot1, tot2... with tot1=2,
> tot2=4...) in my environment?
>
> TY for any help
>
> ______________________________________________
> [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.
>



--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.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.
Reply | Threaded
Open this post in threaded view
|

Re: writing function

Wu Gong
In reply to this post by arnaud Gaboury
## Create a function to assign a series of values to a list of objects
## The assign function can only assign one value (could be a vector) to a name
## Set the environment to be global, otherwise the objects can't be used outside the function
## List objects that have been created

toto <- function(x,y) {
        for (i in x:y){
                assign(paste("tot", i, sep=""), i*2, envir = .GlobalEnv)
                }}
toto(3,7)
ls(pattern = "^tot.$")