Hi I am trying to read a data frame from a text editor in to R. I want some of the columns to be read in as "character" not numeric. I figured that I can do that by using "colClasses" in "read.table" command. However, I couldn't find out how to use "colClasses". e.g. say I have 5 column in the data file. I want 1st and 3rd column to be read in as character. How can I define this using colClasses? (or is there a better way to do what I want?) Thanks. Zeynep [[alternative HTML version deleted]]
On Mon, 11 Oct 2004 16:21:31 -0400, "Kalaylioglu, Zeynep (IMS)" <KalayliogluZ at imsweb.com> wrote:>Hi >I am trying to read a data frame from a text editor in to R. I want some >of the columns to be read in as "character" not numeric. >I figured that I can do that by using "colClasses" in "read.table" >command. However, I couldn't find out how to use >"colClasses". e.g. say I have 5 column in the data file. I want 1st and >3rd column to be read in as character. How can I define >this using colClasses? (or is there a better way to do what I want?)The help page for read.table looks reasonably clear to me. Wouldn't colClasses = c('character', 'numeric', 'character', 'numeric', 'numeric') work? Duncan Murdoch
When using colClasses you need to specify the types of all the columns. So assuming the other columns are numeric data, you could set colClasses = c("character", "numeric", "character", "numeric", "numeric") -roger Kalaylioglu, Zeynep (IMS) wrote:> Hi > I am trying to read a data frame from a text editor in to R. I want some > of the columns to be read in as "character" not numeric. > I figured that I can do that by using "colClasses" in "read.table" > command. However, I couldn't find out how to use > "colClasses". e.g. say I have 5 column in the data file. I want 1st and > 3rd column to be read in as character. How can I define > this using colClasses? (or is there a better way to do what I want?) > > Thanks. > Zeynep > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
I would try. colClasses=c("character","numeric","character","numeric","numeric") /E Kalaylioglu, Zeynep (IMS) wrote:>Hi >I am trying to read a data frame from a text editor in to R. I want some >of the columns to be read in as "character" not numeric. >I figured that I can do that by using "colClasses" in "read.table" >command. However, I couldn't find out how to use >"colClasses". e.g. say I have 5 column in the data file. I want 1st and >3rd column to be read in as character. How can I define >this using colClasses? (or is there a better way to do what I want?) > >Thanks. >Zeynep > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >-- Dipl. bio-chem. Witold Eryk Wolski MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin _ tel: 0049-30-83875219 __("< 'v' http://www.molgen.mpg.de/~wolski \__/ / \ mail: witek96 at users.sourceforge.net ^^ w w wolski at molgen.mpg.de
"Kalaylioglu, Zeynep (IMS)" <KalayliogluZ at imsweb.com> writes:> Hi > I am trying to read a data frame from a text editor in to R. I want some > of the columns to be read in as "character" not numeric. > I figured that I can do that by using "colClasses" in "read.table" > command. However, I couldn't find out how to use > "colClasses". e.g. say I have 5 column in the data file. I want 1st and > 3rd column to be read in as character. How can I define > this using colClasses? (or is there a better way to do what I want?)The easiest is probably read.table(....as.is=c(1,3)....) but you could also do cC <- rep(NA,5) cC[c(1,3)] <- "character" read.table(....colClasses=cC...) which would be more readily generalized to "stranger" data types. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Seemingly Similar Threads
- tree version 1.0-16
- colClasses: supressed 'NA'
- A couple of issues with colClasses/setAs
- Any way to get read.table.ffdf() (in the ff package) to pass colClasses or comment.char parameters through to read.fwf() ?
- read.table(..., header == FALSE, colClasses = <vector with names attribute>)