Re: [R] create variables through a loop

2011-09-22 Thread Changbin Du
HI, Dennis, Thanks for the helps! All roads to Rome, appreciated! On Thu, Sep 22, 2011 at 2:54 PM, Dennis Murphy wrote: > Hi: > > Here's one approach with package reshape2. I copied the same data > frame 10 times to illustrate the idea, but as long as your data frames > have the same structur

Re: [R] create variables through a loop

2011-09-22 Thread Dennis Murphy
Hi: Here's one approach with package reshape2. I copied the same data frame 10 times to illustrate the idea, but as long as your data frames have the same structure (same variables, same dimension), this should work. library('plyr') library('reshape2') # Use your example data as the template: ds

Re: [R] create variables through a loop

2011-09-22 Thread David Winsemius
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 1C-7SARK 1 8

Re: [R] create variables through a loop

2011-09-22 Thread Changbin Du
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 1C-7S

Re: [R] create variables through a loop

2011-09-22 Thread Changbin Du
Thank, Jean, appreciated your help! On Thu, Sep 22, 2011 at 10:52 AM, Jean-Christophe BOUËTTÉ < jcboue...@gmail.com> wrote: > 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): > >

Re: [R] create variables through a loop

2011-09-22 Thread Changbin Du
Thanks so much, Michael! head(first) probe_name chr_id position array1 1C-7SARK 1 849467 10 2C-4WYLN 1 854278 10 3C-3BFNY 1 854471 10 4C-7ONNE 1 874460 10 5C-6HYCN 1 874571 10 6C-7SCGC 1 874609 10 for( i i

Re: [R] create variables through a loop

2011-09-22 Thread Jean-Christophe BOUËTTÉ
On how to use named vs. positional arguments, you can also have a look at section 2.3 of "an introduction to R". 2011/9/22 Jean-Christophe BOUËTTÉ : > 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

Re: [R] create variables through a loop

2011-09-22 Thread Jean-Christophe BOUËTTÉ
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

Re: [R] create variables through a loop

2011-09-22 Thread R. Michael Weylandt
Actually, I think you can make that even easier: first.out <- cbind(first, result.fun) Michael Weylandt On Thu, Sep 22, 2011 at 1:37 PM, 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

Re: [R] create variables through a loop

2011-09-22 Thread R. Michael Weylandt
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 general

Re: [R] create variables through a loop

2011-09-22 Thread Changbin Du
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

Re: [R] create variables through a loop

2011-09-22 Thread R. Michael Weylandt
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 general

[R] create variables through a loop

2011-09-22 Thread Changbin Du
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 1C-7SARK 1 849467 10 2C-4WYLN 1 854278 10 3C-3BFNY 1 854471 10 4C-7ONNE