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