[R] Using cbind to combine data frames and preserve header/names

David Winsemius dwinsemius at comcast.net
Sat Nov 17 17:25:52 CET 2012


On Nov 16, 2012, at 9:39 PM, Brian Feeny wrote:

> I have a dataframe that has a header like so:
>
> class	value1	value2	value3
>
> class is a factor
>
> the actual values in the columns value1, value2 and value3 are  
> 0-255, I wish to binarize these using biclust.
> I can do this like so:
>
> binarize(dataframe[,-1])
>
> this will return a dataframe, but then I lose my first column class,  
> so I thought I could combine it like so:
>
> dataframe <- cbind(dataframe$label, binarize(dataframe[,-1]))

There is no column with the name "label". There is also no function  
named "label" in base R although I cannot speak about biclust. Even if  
there were, you cannot apply functions to data.frames with the "$"  
function.
>
> but then I lose my header (names).............how can I do the above  
> operation and keep my header in tact?
>
> Basically i just want to binarize everything but the first column  
> (since its a factor column and not numeric).

I have no idea how 'binarize' works but if you wanted to 'defactorize'  
a factor then you should learn to use 'as.character' to turn factors  
into character vectors. Perhaps:

dfrm <- cbind( as.character(dataframe[1]), binarize(dataframe[,-1]))

You should make sure this is still a dataframe since cbind.default   
returns a matrix and this would be a character matrix. I'm taking your  
word that the second argument is a dataframe, and that would mean the  
cbind.data.frame method would be dispatched.

It is a rather unfortunate practice to call your dataframes  
"dataframe" and also bad to name your columns "class" since the first  
is a fundamental term and the second a basic function. If you persist,  
people will start talking to you about dogs named "Dog".



David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list