R-help, I have a data frame (df) and I want to add some columns whose names should correspond to the "i" index in the loop below. for(i in 1:10) { df$eval(paste("St", as.character(i), sep = "" )) <- ObJeCt[i] } An error message comes out : "Error: attempt to apply non-function" How can I get around this? I could do something like : df$St2 <- NA df$St3 <- NA dft$St4 < -NA .. and afterwards assign the results of the loop above to the columns df$St2,df$St3,,,,,,,,,,, The problem is that my object "ObJeCt[i]" may change in size and definition and therefore a way to systematize the task would be desirable. Thanks in advance> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 4.1 year 2006 month 12 day 18 svn rev 40228 language R version.string R version 2.4.1 (2006-12-18)
Have you tried something like df <- cbind(df, ObJeCT[, 1:10]) Cheers, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch > [mailto:r-help-bounces op stat.math.ethz.ch] Namens Luis Ridao Cruz > Verzonden: woensdag 21 maart 2007 16:14 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] for loop in assigning column names > > R-help, > > I have a data frame (df) and I want to add some columns whose > names should correspond to the "i" index in the loop below. > > for(i in 1:10) > { > df$eval(paste("St", as.character(i), sep = "" )) <- ObJeCt[i] } > > An error message comes out : > > "Error: attempt to apply non-function" > > How can I get around this? > > I could do something like : > > df$St2 <- NA > df$St3 <- NA > dft$St4 < -NA > .. > > and afterwards assign the results of the loop above to the > columns df$St2,df$St3,,,,,,,,,,, > > The problem is that my object "ObJeCt[i]" may change in size > and definition and therefore a way to systematize the task > would be desirable. > > Thanks in advance > > > version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 4.1 > year 2006 > month 12 > day 18 > svn rev 40228 > language R > version.string R version 2.4.1 (2006-12-18) > > ______________________________________________ > R-help op stat.math.ethz.ch 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. >
try df.new <- cbind(df, ObJeCt[1:10]) names(df.new) <- c(names(df), paste("St", 1:10, sep = "")) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Luis Ridao Cruz" <Luisr at frs.fo> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, March 21, 2007 4:14 PM Subject: [R] for loop in assigning column names> R-help, > > I have a data frame (df) and I want to add some columns whose names > should correspond to the "i" index in the loop below. > > for(i in 1:10) > { > df$eval(paste("St", as.character(i), sep = "" )) <- ObJeCt[i] > } > > An error message comes out : > > "Error: attempt to apply non-function" > > How can I get around this? > > I could do something like : > > df$St2 <- NA > df$St3 <- NA > dft$St4 < -NA > .. > > and afterwards assign the results of the loop above > to the columns df$St2,df$St3,,,,,,,,,,, > > The problem is that my object "ObJeCt[i]" may change in size > and definition and therefore a way to systematize the task would > be desirable. > > Thanks in advance > >> version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 4.1 > year 2006 > month 12 > day 18 > svn rev 40228 > language R > version.string R version 2.4.1 (2006-12-18) > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm