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
Try this: paste("tot", 4:16, sep = "") Or: func <- function(x,y) { paste("tot", x:y, sep = "") } func(4,16) ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 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 <arnaud.gaboury@gmail.com>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 > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
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 > > ______________________________________________ > 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. > >-- 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 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
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: Tal.Galili@gmail.com | 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 <ivan.calandra@uni-hamburg.de> 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 > > ______________________________________________ > 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. > > > > -- > 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 > ivan.calandra@uni-hamburg.de > > ********** > http://www.for771.uni-bonn.de > http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php > > > ______________________________________________ > 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. >-- 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 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]
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 <arnaud.gaboury at gmail.com> 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 > > ______________________________________________ > 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. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/
## 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.$") ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/writing-function-tp2224781p2225249.html Sent from the R help mailing list archive at Nabble.com.
Dear group, Here is my environment after I run a function, myfun()>myfun() > ls()[1] "allcon" "avprix16" "DailyPL100416" "DailyPL100419" "DailyPL100420" "l" "ll" "myl" "PL" "PLdaily" "PLglobal" "PLmonthly" [13] "Pos100415" "Pos100416" "Pos100419" "Pos100420" "pose15" "pose16" "position" "r" "result" "sel" "select" "Trad100415" [25] "Trad100416" "Trad100419" "Trad100420" "trade" "tt" "value" "w" "zz" Elements "DailyPL100416" "DailyPL100419" "DailyPL100420" are data frames created by a function, myfun(). If then I pass this line manually at the prompt:>dd<-data.frame(do.call(rbind,mget(grep("DailyPL",ls(),value=TRUE),envir=.GlobalEnv)),row.names=NULL) Here is what I get, wich is the expected result : dd <- structure(list(DESCRIPTION = structure(c(2L, 5L, 6L, 7L, 9L, 11L, 12L, 15L, 14L, 16L, 1L, 10L, 3L, 4L, 13L, 8L, 17L, 2L, 3L, 5L, 7L, 9L, 11L, 12L, 13L, 14L, 16L, 18L, 8L, 6L, 10L, 15L, 5L, 19L, 9L, 10L, 11L, 12L, 13L, 14L, 16L, 2L, 3L, 20L, 18L, 7L, 21L), .Label = c("COFFEE C Jul/10", "COPPER May/10", "CORN Jul/10", "CORN May/10", "COTTON NO.2 Jul/10", "CRUDE OIL miNY May/10", "GOLD Jun/10", "HENRY HUB NATURAL GAS May/10", "ROBUSTA COFFEE (10) Jul/10", "SILVER May/10", "SOYBEANS Jul/10", "SPCL HIGH GRADE ZINC USD", "STANDARD LEAD USD", "SUGAR NO.11 Jul/10", "SUGAR NO.11 May/10", "WHEAT Jul/10", "WHEAT May/10", "CRUDE OIL miNY Jun/10", "HENRY HUB NATURAL GAS Jun/10", "PALLADIUM Jun/10", "PLATINUM Jul/10"), class = "factor"), PL = c(3500, -1874.99999999999, -2612.50000000003, -2169.99999999998, -680, 425, 1025, 1008.00000000000, -3057.59999999999, 3212.5, -1781.25000000001, -2265.0, 75, -387.5, 2950, 490.000000000013, 0, 2612.49999999998, -4162.5, 279.99999999998, 589.999999999964, -3670, -1212.5, 887.5, -625, 3976.00000000000, -7250, -1112.50000000000, -289.999999999999, -1414.99999999999, 275, -526.400000000003, 6914.99999999999, -19.9999999999978, -2330, 54.9999999999955, 1725, -3012.5, -5175, -1769.60000000001, 3787.5, -537.500000000009, 175, -5330.00000000001, 362.49999999999, 590.000000000009, -2995.00000000000)), .Names c("DESCRIPTION", "PL"), row.names = c(NA, -47L), class = "data.frame") If I add this same line at the end of my function , here is what I get:> myfun()data frame with 0 columns and 0 rows #dd doesn't return What is wrong? Thank you for help.