Great! I got it. You're right, I misinterpreted Duncan's suggestion. Thanks a lot to both of you...
Matteo On 24 November 2011 02:47, Rolf Turner-3 [via R] <ml-node+s789695n4102338...@n4.nabble.com> wrote: > On 24/11/11 09:23, matric wrote: >> Thanks Duncan, >> I knew it. But if I use the complete variable name, I'll have far too >> many arguments for my function.... > > Did you understand Duncan's post? Â He told you that > > Â Â Â df[,var] > > would work, whereas df$var doesn't. Â So he gave you a solution > to the problem whereby you ***don't*** have to use the ``complete > variable name''. > > The ``var'' in the foregoing is the object (character string) that you > constructed using paste(). Â I think you are misinterpreting Duncan > to be suggesting that you use df[,"x_narrow"], df[,"x_wide"] etc. > explicitly. > > Â Â Â cheers, > > Â Â Â Â Â Rolf Turner > > P. S. Â But anyway, as Duncan said in a follow-up post, a new design is > probably > called for. > > Â Â Â Â Â R. T. >> On 23 November 2011 20:59, Duncan Murdoch-2 [via R] >> <[hidden email]> Â wrote: >>> On 23/11/2011 2:29 PM, matric wrote: >>>> Hi, >>>> I'd like to create a function that accepts as arguments a string that is >>>> to >>>> be substituted within a variable name. For instance, suppose I have a >>>> data >>>> frame df: >>>> >>>> df<-data.frame(x_narrow=c(rnorm(100,0,1)),x_wide=c(rnorm(100,0,10))) >>>> >>>> What I have in mind is something like: >>>> >>>> f<- function(string){ >>>> var = paste("x_",string,sep = "") >>>> df$var >>>> } >>>> >>>> which does not work. Any suggestion for modifications? Thanks in >>>> advance, >>> For indexing a dataframe, use the name as the column index: >>> >>> df[, var] >>> >>> will work. >>> >>> Duncan Murdoch > ______________________________________________ > [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. > > > ________________________________ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/character-substitution-within-a-variable-name-tp4101154p4102338.html > To unsubscribe from character substitution within a variable name, click > here. > NAML -- View this message in context: http://r.789695.n4.nabble.com/character-substitution-within-a-variable-name-tp4101154p4103700.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
______________________________________________ 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.