Try:

df1 = read.csv('result.csv', header=TRUE)

df1

Jason

On 12/31/2010 03:19 AM, Amy Milano wrote:
Please read - http://127.0.0.1:24408/library/base/html/colnames.html

I am trying to give an example. I am not a programmer and I am sure the R 
stalwarts will have better ways to do it.

# Suppose

df1 = read.csv('result.csv')

where (say e.g.) result.csv is as follows

result.csv

      var1   var2
1      25     45
2      80    132
3     135     11

country = c("US", "Canada")

colnames(df1)<- country

HTH

Amy


--- On Fri, 12/31/10, Vincy Pyne<vincy_p...@yahoo.ca>  wrote:

From: Vincy Pyne<vincy_p...@yahoo.ca>
Subject: [R] Changing column names
To: r-help@r-project.org
Date: Friday, December 31, 2010, 6:07 AM

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


______________________________________________
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