[R] for loop in assigning column names

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Wed Mar 21 16:23:23 CET 2007


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



More information about the R-help mailing list