[R] create variables through a loop
David Winsemius
dwinsemius at comcast.net
Thu Sep 22 20:20:44 CEST 2011
On Sep 22, 2011, at 2:06 PM, Changbin Du wrote:
> HI, Michael,
>
> The following codes are great!
>
> first.out <- do.call("cbind", list(first, result.fun))
Would this have been easier?
> dat[ , c(1:3, rep(4, 5))]
probe_name chr_id position array1 array1.1 array1.2 array1.3
1 C-7SARK 1 849467 10 10 10 10
2 C-4WYLN 1 854278 10 10 10 10
3 C-3BFNY 1 854471 10 10 10 10
4 C-7ONNE 1 874460 10 10 10 10
5 C-6HYCN 1 874571 10 10 10 10
6 C-7SCGC 1 874609 10 10 10 10
array1.4
1 10
2 10
3 10
4 10
5 10
6 10
With the obvious substitution of 45 as the second argument to `rep`
where you currently see 5.
>
> colnames(first.out) <- c(colnames(first), paste("array",
> seq(length(result.fun)), sep=""))
They may or may not need to be renamed.
--
David.
>
>> head(first.out)
> probe_name chr_id position array1 array2 array3 array4 array5 array6
> array7
> 1 C-7SARK 1 849467 10 10 10 10 10 10
> 10
> 2 C-4WYLN 1 854278 10 10 10 10 10 10
> 10
> 3 C-3BFNY 1 854471 10 10 10 10 10 10
> 10
> 4 C-7ONNE 1 874460 10 10 10 10 10 10
> 10
> 5 C-6HYCN 1 874571 10 10 10 10 10 10
> 10
> 6 C-7SCGC 1 874609 10 10 10 10 10 10
> 10
> array8 array9 array10 array11 array12 array13 array14 array15 array16
> array17
> 1 10 10 10 10 10 10 10 10
> 10
> 10
> 2 10 10 10 10 10 10 10 10
> 10
>
snipped extraneous output.
> Appreciated your great helps!
>
>
>
>
> On Thu, Sep 22, 2011 at 10:55 AM, Changbin Du <changbind at gmail.com>
> wrote:
>
>> Thanks so much, Michael!
>>
>>
>> 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
>>
>> for( i in 2:3) {
>> + label <- paste("array", i, sep="")
>> + assign(label, value = result.fun[[i-1]] )
>> + first <- cbind(first, get(label))
>> + }
>>
>> head(first)
>> probe_name chr_id position array1 get(label) get(label)
>>
>> 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 can use the codes to change the columns names.
>>
>> colnames(first.out) <- c(colnames(first), paste("array",
>> seq(length(result.fun)), sep=""))
>>
>>
>> I am running:
>>
>>> first.out <- do.call("cbind", list(first, result.fun))
>>
>> and
>>
>>> first.out <- cbind(first, result.fun)
>>
>> IT has been 10 mins, and I will let you know the results.
>>
>> Thanks so much for the great helps!
>>
>>
>>
>>
>>
>>
>> On Thu, Sep 22, 2011 at 10:37 AM, R. Michael Weylandt <
>> michael.weylandt at gmail.com> wrote:
>>
>>> There are a few ways to proceed from here. If you are really
>>> committed to
>>> this loop + assign idea, I'd provide the following code:
>>>
>>> for( i in 2:3) {
>>> label <- paste("array", i, sep="")
>>> assign(label, value = result.fun[[i-1]] )
>>> first <- cbind(first, get(label))
>>> }
>>>
>>> However, this is generally pretty inefficient. Why not something
>>> more like
>>> the following?
>>>
>>> first.out <- do.call("cbind", list(first, result.fun))
>>>
>>> If you need the names to be "arrayi" you can add this line:
>>> colnames(first.out) <- c(colnames(first), paste("array",
>>> seq(length(result.fun)), sep=""))
>>>
>>> I'm unable to test this on your (unprovided) data, but here's an
>>> example
>>> of how this works:
>>>
>>> first = data.frame(x = 1:3, y = 6:8, z = 11:13)
>>>
>>> a = data.frame(a = 1:3)
>>> b = data.frame(b = 4:6)
>>> result.fun = list(a,b)
>>>
>>> first.out <- do.call("cbind", list(first, result.fun))
>>> print(first.out)
>>>
>>> which provides this output.
>>>
>>> x y z a b
>>> 1 1 6 11 1 4
>>> 2 2 7 12 2 5
>>> 3 3 8 13 3 6
>>>
>>> More generally, you really should read about how arguments and
>>> assignments
>>> work in R. See, e.g., 8.2.26 in the R inferno.
>>>
>>> Michael Weylandt
>>>
>>>
>>> On Thu, Sep 22, 2011 at 1:21 PM, Changbin Du <changbind at gmail.com>
>>> wrote:
>>>
>>>> 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
>>>>>> --
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list