[R] create variables through a loop

Jean-Christophe BOUËTTÉ jcbouette at gmail.com
Thu Sep 22 19:52:41 CEST 2011


Hi,
It's hard to provide you with working code when you don't provide a
reproducible example, but do you really need to create variables? What
about (untested):

for (i in 1:2) {
  first <-cbind(first, result.fun[[i]])
}

you will then have to look at
names(first)
and change the last part of it to
paste("array",2:3,sep="")

Hope that works!
JC

2011/9/22 Changbin Du <changbind at gmail.com>:
> HI, Michael,
>
> I tried use x and got the following:
>
>> for (i in 2:3) {
> +
> + assign(x=paste("array", i, sep=""), value=result.fun[[i-1]])
> +
> + first <-cbind(first, x)
> +
> + }
> *Error in cbind(first, x) : object 'x' not found
> *
>
> But I checked the
>  ls()
>     "array2"       "array3"    were created.
>
> Can I put them into the first data set by loop, or manually?
>
> Thanks!
>
>
> P.S   I search the similar codes from google and can not work as I expected.
>
> Thanks!
>
>
>
> On Thu, Sep 22, 2011 at 10:11 AM, R. Michael Weylandt <
> michael.weylandt at gmail.com> wrote:
>
>> There is no "lab=" argument for assign() hence the error. Did someone
>> provide you with example code that suggested such a thing? remove lab=
>> entirely or replace it with x= to make your code work. More generally type
>> ?assign or args(assign) to see what the arguments for a function are.
>>
>> More generally, this sort of thing may be best handled in a list rather
>> than an set of independent variables.
>>
>> Michael Weylandt
>>
>> On Thu, Sep 22, 2011 at 1:07 PM, Changbin Du <changbind at gmail.com> wrote:
>>
>>> HI, Dear R community,
>>>
>>> I am trying to created new variables and put into a data frame through a
>>> loop.
>>>
>>> My original data set:
>>>
>>> head(first)
>>>  probe_name chr_id position array1
>>> 1    C-7SARK      1   849467     10
>>> 2    C-4WYLN      1   854278     10
>>> 3    C-3BFNY      1   854471     10
>>> 4    C-7ONNE      1   874460     10
>>> 5    C-6HYCN      1   874571     10
>>> 6    C-7SCGC      1   874609     10
>>>
>>>
>>> I have 48 other array data from a list result.fun
>>> array2=result.fun[[1]]
>>> array3=result.fun[[2]]
>>> .
>>> .
>>>
>>> I want the following results:
>>>
>>>  probe_name chr_id position array1 array2 array3
>>> 1    C-7SARK      1   849467     10     10       10
>>> 2    C-4WYLN      1   854278     10     10       10
>>> 3    C-3BFNY      1   854471     10      10       10
>>> 4    C-7ONNE      1   874460     10     10       10
>>> 5    C-6HYCN      1   874571     10     10       10
>>> 6    C-7SCGC      1   874609     10     10       10
>>>
>>>
>>> I used the following codes:
>>>
>>> for (i in 2:3) {
>>>
>>> assign(lab=paste("array", i, sep=""), value=result.fun[[i-1]])
>>>
>>> first <-cbind(first, lab)
>>>
>>> }
>>>
>>> *Error in assign(lab = paste("array", i, sep = ""), value = result.fun[[i
>>> -
>>> :
>>>  unused argument(s) (lab = paste("array", i, sep = ""))*
>>>
>>>
>>> Can anyone give some hits or helps?
>>>
>>> Thanks so much!
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Sincerely,
>>> Changbin
>>> --
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>
>
> --
> Sincerely,
> Changbin
> --
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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