[R] Changing column names
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Sat Jan 1 01:32:37 CET 2011
You don't give us much to go on, but some variant of
country <- c("US", "France", "UK", "NewZealand", "Germany", "Austria", "Italy", "Canada")
result <- read.csv("result.csv", header = FALSE)
names(result) <- country
should do what you want.
________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Vincy Pyne [vincy_pyne at yahoo.ca]
Sent: 31 December 2010 16:07
To: r-help at r-project.org
Subject: [R] Changing column names
Dear R helpers
Wish you all a very Happy and Prosperous New Year 2011.
I have following query.
country = c("US", "France", "UK", "NewZealand", "Germany", "Austria", "Italy", "Canada")
Through some other R process, the result.csv file is generated as
result.csv
var1 var2 var3 var4 var5 var6 var7 var8
1 25 45 29 92 108 105 65 56
2 80 132 83 38 38 11 47 74
3 135 11 74 56 74 74 74 29
I need the country names to be column heads i.e. I need an output like
> result_new
US France UK NewZealand Germany Austria Italy Canada
1 25 45 29 92 108 105 65 56
2 80 132 83 38 38 11 47 74
3 135 11 74 56 74 74 74 29
The number of countries i.e. length(country) matches with total number of variables (i.e. no of columns in 'result.csv').
One way of doing this is to use country names as column names while writing the 'result.csv' file.
write.csv(data.frame(US = ..........., France = .......), 'result.csv', row.names = FALSE)
However, the problem is I don't know in what order the country names will appear and also there could be addition or deletion of some country names. Also, if there are say 150 country names, the above way (i.e. writing.csv) of defining the column names is not practical.
Basically I want to change the column heads after the 'result.csv' is generated.
Kindly guide.
Regards
Vincy
[[alternative HTML version deleted]]
More information about the R-help
mailing list