[R] for loop in assigning column names
Dimitris Rizopoulos
dimitris.rizopoulos at med.kuleuven.be
Wed Mar 21 16:27:05 CET 2007
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
More information about the R-help
mailing list