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]]
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Vincy Pyne > Sent: Thursday, December 30, 2010 10:07 PM > 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 > >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
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_pyne@yahoo.ca> wrote:
From: Vincy Pyne <vincy_pyne@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
[[alternative HTML version deleted]]
-----Inline Attachment Follows-----
______________________________________________
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.
[[alternative HTML version deleted]]
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]]