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])) 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). Thank you for any help you can give me, I am relatively new to R. Brian
Using cbind to combine data frames and preserve header/names
4 messages · Brian Feeny, Rainer Schuermann, David Winsemius
Not sure where the problem is? Since you did not provide sample data, I took the iris data set and converted it to your structure: x <- cbind( iris[5], iris[1:3] ) head( x ) Species Sepal.Length Sepal.Width Petal.Length 1 setosa 5.1 3.5 1.4 2 setosa 4.9 3.0 1.4 3 setosa 4.7 3.2 1.3 4 setosa 4.6 3.1 1.5 5 setosa 5.0 3.6 1.4 6 setosa 5.4 3.9 1.7 Does that look like your data? If so, xbin <- cbind( x[1], binarize( x[2:4] ) ) gives a result that should look just like what you want: head( xbin ) Species Sepal.Length Sepal.Width Petal.Length 1 setosa 1 0 0 2 setosa 1 0 0 3 setosa 1 0 0 4 setosa 1 0 0 5 setosa 1 0 0 6 setosa 1 0 0 Using xbin <- cbind( x$Species, binarize( x[-1] ) ) doesn't make a difference. Or did I not understand your problem well? Rgds, Rainer
On Saturday 17 November 2012 00:39:02 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])) 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). Thank you for any help you can give me, I am relatively new to R. Brian
______________________________________________ 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.
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
David and Rainer,
Thank you both for your responses, you got me on track, I ended up just doing like so:
trainset <- read.csv('train.csv',head=TRUE)
trainset[,-1] <- binarize(trainset[,-1])
trainset$label <- as.factor(trainset$label)
I appreciate your help
Brian
On Nov 17, 2012, at 11:25 AM, David Winsemius wrote:
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