[R] replace column headers

Esmail Bonakdarian esmail.js at gmail.com
Tue Jun 17 13:09:51 CEST 2008


Paul Adams wrote:
> 
> 
> Hello everyone,=I have a question as to how to remove the column headers
> in a data file and then replace those with titles from another file in this case
> the file labeled ann  (
> in which the titles are all in one column).

Maybe this will help partially.

I am not sure on how to read your new column names from a file into a vector
but if you can get them into that format this should let you accomplish your
goal.

Esmail

----

 > load("Data/simpleTestData2.rda")

 > df
        Y X1   X2   X3 X4 X5 X6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 > colnames(df)
[1] "Y"  "X1" "X2" "X3" "X4" "X5" "X6"

 > altnames=c("A", "B1", "B2", "B3", "B4", "B5", "B6")

 > colnames(df)=altnames

 > df
        A B1   B2   B3 B4 B5 B6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 > save("df", file="example.rda")

# new file will contain the new column headers.



More information about the R-help mailing list