Re: [R] Column name defined by function variable

From: Ivan Calandra <ivan.calandra_at_uni-hamburg.de>
Date: Thu, 17 Jun 2010 11:54:10 +0200

Now that I take a better look at your function, I don't understand everything, but this should work (it works for me, if I understood correctly at least what you're looking for):

add.col <- function(df, new.col, name) {

     n.row <- dim(df)[1]
     length(new.col) <- n.row
     test <- cbind(df, new.col)
     names(test) <- c(names(df), name)
     return(test)

}

There might be better/easier/nicer solutions.

HTH,
Ivan

Le 6/17/2010 11:25, Ralf B a écrit :
> Sorry, its late and I am getting tired ;)
>
> I modified based on your suggestion:
>
> #combine data
> add.col<- function(df, new.col, name) {
> n.row<- dim(df)[1]
> length(new.col)<- n.row
> names(new.col)<- name
> cbind(df, new.col)
> }
> data<- data.frame(stuff1=as.numeric(d2$points))
> data<- add.col(data, as.numeric(d1$morepoints), "stuff2")
>
> but the column in the data frame is still called 'new.col' and not 'stuff2'.
>
> Any further ideas?
>
> Best,
> Ralf
>
>
>
> On Thu, Jun 17, 2010 at 5:14 AM, Ivan Calandra
> <ivan.calandra_at_uni-hamburg.de> wrote:
>
>> Hi,
>>
>> I haven't check much of what you wrote, so just a blind guess. What about in
>> the function's body before cbind():
>> names(new.col)<- "more stuff"
>> ?
>>
>> HTH,
>> Ivan
>>
>> Le 6/17/2010 11:09, Ralf B a écrit :
>>
>>> Hi all,
>>>
>>> probably a simple problem for you but I am stuck.
>>>
>>> This simple function adds columns (with differing length) to data frames:
>>>
>>> add.col<- function(df, new.col) {
>>> n.row<- dim(df)[1]
>>> length(new.col)<- n.row
>>> cbind(df, new.col)
>>> }
>>>
>>> Now I would like to extend that method. A new parameter 'name' shouild
>>> allow people to pass in a name for that new column. Is that possible
>>> and how can this be achieved?
>>>
>>> Example:
>>>
>>> myData<- data.frame(c(1,2,3))
>>> add.col(myData, c(5,6,7,8), 'more stuff')
>>>
>>> adds a new column named 'more stuff' to the dataframe myData.
>>>
>>>
>>> Any ideas?
>>>
>>> Best,
>>> Ralf
>>>
>>> ______________________________________________
>>> R-help_at_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.
>>>
>>>
>>>
>> --
>> Ivan CALANDRA
>> PhD Student
>> University of Hamburg
>> Biozentrum Grindel und Zoologisches Museum
>> Abt. Säugetiere
>> Martin-Luther-King-Platz 3
>> D-20146 Hamburg, GERMANY
>> +49(0)40 42838 6231
>> ivan.calandra_at_uni-hamburg.de
>>
>> **********
>> http://www.for771.uni-bonn.de
>> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>>
>> ______________________________________________
>> R-help_at_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.
>>
>>
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra_at_uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

______________________________________________
R-help_at_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.
Received on Thu 17 Jun 2010 - 09:56:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jun 2010 - 10:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive