> -----Original Message-----
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of Vincy Pyne
> Sent: Thursday, December 30, 2010 10:07 PM
> To: r-help@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
> 
> 

The posting guide asks for a self-contained, reproducible example.  This is a 
case where it seems this would be mandatory.  First, 

country = c("US", "France", "UK", "NewZealand", "Germany", "Austria", "Italy", 
"Canada")

is not a query.  We don't know how you are using this vector, and you tell us 
that the order in which the variables are returned may change, but give us no 
clue as to how they might change.  So there is no possible way anyone can give 
you any assistance without further information.  Give us a sample data frame 
with a few columns and few rows and show how you are generating your results, 
and I am sure someone will be able to help you out.  But you have to help us. 
:-)

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
R-help@r-project.org 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.

Reply via email to