Re: [R] Change data frame column names

2009-07-15 Thread Tom Liptrot

Thanks all,

I used Gavins approach - unlisting the titles and the replacing names, as my 
titles were stored as factors in the data frame as that was the way they were 
imported...

Tom


> Subject: Re: [R] Change data frame column names
> From: gavin.simp...@ucl.ac.uk
> To: tomlipt...@hotmail.com
> CC: r-help@r-project.org
> Date: Wed, 15 Jul 2009 16:12:32 +0100
> 
> On Wed, 2009-07-15 at 14:35 +, Tom Liptrot wrote:
> > 
> > 
> > 
> > Hi R helpers,
> > 
> > I have a data frame and I want to change the column names to names I have 
> > held in another data frame, but I am having difficulty. All data framnes 
> > are large so i can't input manually. Below is what i have tried so far:
> > 
> > df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
> > coltitles<-data.frame(v1="col number one", v2="col number two", v3="col 
> > number three", v4="col number four")
> > 
> > ##first attempt
> > 
> > names(df)<-coltitles
> > names(df)
> > [1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return 
> > [1] "col number one" "col number two" "col number three" "col number four"
> > 
> > 
> > 
> > ##second attempt
> > 
> > coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a 
> > character vector after looking at help
> > is.vector(coltitles)
> > [1] TRUE
> > names(df)<-coltitles
> > names(df)
> > [1] "1" "1" "1" "1"   ###again not what I wanted
> > 
> > How can I convert the column names?
> 
> This seems a bit of a strange way to go about things, but if needs must:
> 
> > names(df) <- unlist(coltitles)
> > df
>   col number one col number two col number three col number four
> 1  1  23   4
> 2  2  34   5
> 3  3  45   6
> 4  4  56   7
> 5  5  67   8
> > names(df)
> [1] "col number one"   "col number two"   "col number three" "col number four"
> 
> If your data frame with the names in the one (and only) row (why store
> this as a one row df and not a character vector??? [1]) then by
> unlisting it we get a (or something that can be coerced to) a character
> vector of the correct names.
> 
> If coltitles contains more rows, then:
> 
> names(df) <- unlist(coltitles[1,])
> 
> might be more appropriate.
> 
> HTH
> 
> G
> 
> [1] Your df storage of names is s inefficient (for this task):
> > object.size(names(df)) # after running the above code
> 312 bytes
> > object.size(coltitles)
> 2728 bytes
> 
> 
> 
> > 
> > Thanks in advance,
> > 
> > Tom
> > 
> > Beyond Hotmail - see what else you can do with Windows Live. Find out more.
> > _
> > 
> > 
> > [[alternative HTML version deleted]]
> > 
> > __
> > 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.
> -- 
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>  Dr. Gavin Simpson [t] +44 (0)20 7679 0522
>  ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
>  Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
>  Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
>  UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> 

_
[[elided Hotmail spam]]

[[alternative HTML version deleted]]

__
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.


Re: [R] Change data frame column names

2009-07-15 Thread Don MacQueen

Try

names(df) <- as.vector(unlist(coltitles))

This will fail if you have more than one row in your data frame coltitles.

Which is why Duncan Murdoch's second email has a better solution.

Given the difficulty in getting the titles out of the data frame in 
vector form, I would wonder why you're storing them as data in a data 
frame in the first place.


Note also that the default behavior when creating a dataframe is for 
character data to be converted to factors. You can see this with



 class(coltitles$v1)

[1] "factor"

That's part of what gave you weird results. So I would suggest 
looking at the help pages to figure out how to prevent conversion to 
factor.


If you want names(df) to return "col number one" etc, then you have 
to create a data frame whose column names are those. You created a 
dataframe (coltitles) whose names were "v1", "v2", etc.


For example,

 tmp <- data.frame("col number one"=1, "col number 
two"='foobar',check.names=FALSE)

 names(tmp)

[1] "col number one" "col number two"

But simpler and easier to just do as Duncan suggested, and create a 
vector of names.


-Don

At 2:35 PM + 7/15/09, Tom Liptrot wrote:

Hi R helpers,

I have a data frame and I want to change the column names to names I 
have held in another data frame, but I am having difficulty. All 
data framnes are large so i can't input manually. Below is what i 
have tried so far:


df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
coltitles<-data.frame(v1="col number one", v2="col number two", 
v3="col number three", v4="col number four")


##first attempt

names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to 
return [1] "col number one" "col number two" "col number three" "col 
number four"




##second attempt

coltitles<-as.vector(coltitles, mode="character")  ##trying to 
convert to a character vector after looking at help

is.vector(coltitles)
[1] TRUE
names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###again not what I wanted

How can I convert the column names?

Thanks in advance,

Tom

Beyond Hotmail - see what else you can do with Windows Live. Find out more.
_


[[alternative HTML version deleted]]

__
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.



--
--
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

__
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.


Re: [R] Change data frame column names

2009-07-15 Thread Gavin Simpson
On Wed, 2009-07-15 at 14:35 +, Tom Liptrot wrote:
> 
> 
> 
> Hi R helpers,
> 
> I have a data frame and I want to change the column names to names I have 
> held in another data frame, but I am having difficulty. All data framnes are 
> large so i can't input manually. Below is what i have tried so far:
> 
> df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
> coltitles<-data.frame(v1="col number one", v2="col number two", v3="col 
> number three", v4="col number four")
> 
> ##first attempt
> 
> names(df)<-coltitles
> names(df)
> [1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return [1] 
> "col number one" "col number two" "col number three" "col number four"
> 
> 
> 
> ##second attempt
> 
> coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a 
> character vector after looking at help
> is.vector(coltitles)
> [1] TRUE
> names(df)<-coltitles
> names(df)
> [1] "1" "1" "1" "1"   ###again not what I wanted
> 
> How can I convert the column names?

This seems a bit of a strange way to go about things, but if needs must:

> names(df) <- unlist(coltitles)
> df
  col number one col number two col number three col number four
1  1  23   4
2  2  34   5
3  3  45   6
4  4  56   7
5  5  67   8
> names(df)
[1] "col number one"   "col number two"   "col number three" "col number four"

If your data frame with the names in the one (and only) row (why store
this as a one row df and not a character vector??? [1]) then by
unlisting it we get a (or something that can be coerced to) a character
vector of the correct names.

If coltitles contains more rows, then:

names(df) <- unlist(coltitles[1,])

might be more appropriate.

HTH

G

[1] Your df storage of names is s inefficient (for this task):
> object.size(names(df)) # after running the above code
312 bytes
> object.size(coltitles)
2728 bytes



> 
> Thanks in advance,
> 
> Tom
> 
> Beyond Hotmail - see what else you can do with Windows Live. Find out more.
> _
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,  [f] +44 (0)20 7679 0565
 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London  [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

__
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.


Re: [R] Change data frame column names

2009-07-15 Thread Duncan Murdoch

On 7/15/2009 10:35 AM, Tom Liptrot wrote:




Hi R helpers,

I have a data frame and I want to change the column names to names I have held 
in another data frame, but I am having difficulty. All data framnes are large 
so i can't input manually. Below is what i have tried so far:

df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
coltitles<-data.frame(v1="col number one", v2="col number two", v3="col number three", 
v4="col number four")

##first attempt

names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return [1] "col number one" "col 
number two" "col number three" "col number four"


Not sure if my first reply went out; it had an error in it, because I 
misread what you were trying to do.


You want to assign a character vector as names.  You can set it up like 
that originally using


coltitles <- c("col number one", "col number two", "col number three", 
"col number four")


and then your first attempt will work.  If you need to get the names out 
of a dataframe, then use


names(x) <- coltitles[1,]

to select the first row (or select some other row if you want) of the 
dataframe to use as names.


Duncan Murdoch



##second attempt

coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a 
character vector after looking at help
is.vector(coltitles)
[1] TRUE
names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###again not what I wanted

How can I convert the column names?

Thanks in advance,

Tom

Beyond Hotmail - see what else you can do with Windows Live. Find out more.
_


[[alternative HTML version deleted]]

__
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.


__
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.


Re: [R] Change data frame column names

2009-07-15 Thread Duncan Murdoch

On 7/15/2009 10:35 AM, Tom Liptrot wrote:




Hi R helpers,

I have a data frame and I want to change the column names to names I have held 
in another data frame, but I am having difficulty. All data framnes are large 
so i can't input manually. Below is what i have tried so far:

df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
coltitles<-data.frame(v1="col number one", v2="col number two", v3="col number three", 
v4="col number four")


It would be simpler to use

coltitles <- c((v1="col number one", v2="col number two", v3="col number 
three", v4="col number four")


(and the v1=, v2=, etc. are redundant).  Then your first attempt would work.



##first attempt

names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return [1] "col number one" "col 
number two" "col number three" "col number four"


If you are taking titles from one data frame to put on another as in 
your original code, then use


names(df) <- names(coltitles)

Duncan Murdoch




##second attempt

coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a 
character vector after looking at help
is.vector(coltitles)
[1] TRUE
names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###again not what I wanted

How can I convert the column names?

Thanks in advance,

Tom

Beyond Hotmail - see what else you can do with Windows Live. Find out more.
_


[[alternative HTML version deleted]]

__
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.


__
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.


[R] Change data frame column names

2009-07-15 Thread Tom Liptrot




Hi R helpers,

I have a data frame and I want to change the column names to names I have held 
in another data frame, but I am having difficulty. All data framnes are large 
so i can't input manually. Below is what i have tried so far:

df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
coltitles<-data.frame(v1="col number one", v2="col number two", v3="col number 
three", v4="col number four")

##first attempt

names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return [1] 
"col number one" "col number two" "col number three" "col number four"



##second attempt

coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a 
character vector after looking at help
is.vector(coltitles)
[1] TRUE
names(df)<-coltitles
names(df)
[1] "1" "1" "1" "1"   ###again not what I wanted

How can I convert the column names?

Thanks in advance,

Tom

Beyond Hotmail - see what else you can do with Windows Live. Find out more.
_


[[alternative HTML version deleted]]

__
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.