[R] How to create multi variables

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed May 14 06:41:22 CEST 2014


Please do the reading I recommended before posting again. My example was numeric because your example was numeric. For more complicated data, a list is a type of vector that can hold such objects, and you would know this if you had read the intro document.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On May 13, 2014 7:22:08 PM PDT, yuanzhi <yuanzhi.li at usherbrooke.ca> wrote:
>Thank you for your reply.
>
>Yes, there is a problem according to you suggestion.
>What if the value are not numerical, e.g. I want to use the variable to
>store the results of linear regression.
>can I use
>myvec <- vector( "numeric", 10 )
>for ( i in 1:10 ) {
>  myvec[ i ] <- summary(lm(y~x)) # y and x are different values in each
>loop.
>}
>?
>
>you advice seems only to be available when the function left allocates
>a
>numerical value to the variable, what if the function return other type
>of
>objects?
>
>
>
>
>Jeff Newmiller wrote
>> What is wrong with
>> 
>> myvec <- vector( "numeric", 10 )
>> for ( i in 1:10 ) {
>>   myvec[ i ] <- i
>> }
>> 
>> ?
>> 
>> If you are using assign, IMHO you are probably doing whatever you are
>> doing wrong.
>> 
>> If you want named elements, give the vector names:
>> 
>> names( myvec ) <- paste0( "t", 1:10 )
>> 
>> and you can refer to them
>> 
>> myvec[ "t3" ]
>> 
>> Go read the "Introduction to R" document again... particularly the
>> discussion of indexing.
>>
>---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go
>> Live...
>> DCN:<
>
>> jdnewmil at .ca
>
>> >        Basics: ##.#.       ##.#.  Live Go...
>>                                       Live:   OO#.. Dead: OO#.. 
>Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#. 
>> rocks...1k
>>
>---------------------------------------------------------------------------
>
>> Sent from my phone. Please excuse my brevity.
>> 
>> On May 13, 2014 5:47:12 PM PDT, Yuanzhi Li <
>
>> Yuanzhi.Li@
>
>> > wrote:
>>>Hi, everyone
>>>
>>>I want to create a series of variables (e.g. t1, t2..., t10) which
>>>could 
>>>be used in loops. My idea is to use function "assign"
>>>
>>>for (i in 1:10)
>>>{
>>>   assign(paste("t",i,sep=""), FUN) # allocate the value from FUN to 
>>>variable ti
>>>}
>>>
>>>But when I create a vector containing the names of these variables
>and 
>>>want to use the variables according to the subscript, it doesn't
>works.
>>>
>>>t<-noquote(paste("t",1:10,sep=""))
>>>t[1]
>>>t1
>>>it returns only the name of variable t1, but not the value allocated
>to
>>>
>>>t1 by FUN. So what should I do to realize this?
>>>
>>>Or is there any better way to do this?
>>>
>>>Can we define a series of variables which can be used according to
>the 
>>>subscript like
>>>t<-f(t1, t2..., t10),
>>>then we have 10 variables which can be used directly?
>>>for(i in 1:10)
>>>{
>>>  t[i]<-FUN# with the fines variables we can directly assign the
>value 
>>>of FUN to ti
>>>}
>>>These are just my thoughts, I don't know whether there are available
>R 
>>>codes to realized it. I am looking forward any help from you.
>>>
>>>Thanks in advance!
>>>
>>>Yuanzhi
>>>
>>>______________________________________________
>>>
>
>> R-help@
>
>>  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@
>
>>  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.
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-create-multi-variables-tp4690465p4690470.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.



More information about the R-help mailing list