HI, Michael,

The following codes are great!

first.out <- do.call("cbind", list(first, result.fun))

colnames(first.out) <- c(colnames(first), paste("array",
seq(length(result.fun)), sep=""))

> 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
10
3     10     10      10      10      10      10      10      10      10
10
4     10     10      10      10      10      10      10      10      10
10
5     10     10      10      10      10      10      10      10      10
10
6     10     10      10      10      10      10      10      10      10
10
  array18 array19 array20 array21 array22 array23 array24 array25 array26
1      10      10      10      10      10      10      10      10      10
2      10      10      10      10      10      10      10      10      10
3      10      10      10      10      10      10      10      10      10
4      10      10      10      10      10      10      10      10      10
5      10      10      10      10      10      10      10      10      10
6      10      10      10      10      10      10      10      10      10
  array27 array28 array29 array30 array31 array32 array33 array34 array35
1      10      10      10      10      10      10      10      10      10
2      10      10      10      10      10      10      10      10      10
3      10      10      10      10      10      10      10      10      10
4      10      10      10      10      10      10      10      10      10
5      10      10      10      10      10      10      10      10      10
6      10      10      10      10      10      10      10      10      10
  array36 array37 array38 array39 array40 array41 array42 array43 array44
1      10      10      10      10      10      10      10      10      10
2      10      10      10      10      10      10      10      10      10
3      10      10      10      10      10      10      10      10      10
4      10      10      10      10      10      10      10      10      10
5      10      10      10      10      10      10      10      10      10
6      10      10      10      10      10      10      10      10      10
  array45 array46 array47 array48
1      10      10      10      10
2      10      10      10      10
3      10      10      10      10
4      10      10      10      10
5      10      10      10      10
6      10      10      10      10


Appreciated your great helps!




On Thu, Sep 22, 2011 at 10:55 AM, Changbin Du <changb...@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.weyla...@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 <changb...@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.weyla...@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 <changb...@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@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
>>> --
>>>
>>>
>>
>
>
> --
> Sincerely,
> Changbin
>
>


-- 
Sincerely,
Changbin

        [[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.

Reply via email to