[R] read.table and column class

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 15 13:03:26 CEST 2008


On Tue, 15 Apr 2008, jim holtman wrote:

> Define those columns as being character with colClasses and then use
> 'ifelse' to change it to boolean:
>
>> x <- c("", "", "+", "", "+")
>> x
> [1] ""  ""  "+" ""  "+"
>> y <- ifelse(x == "", FALSE, TRUE)
>> y
> [1] FALSE FALSE  TRUE FALSE  TRUE

Or define a custom class with an as() method and use that in colClasses.

>>
>
>
> On Tue, Apr 15, 2008 at 5:10 AM, Johannes Graumann
> <johannes_graumann at web.de> wrote:
>> Hi,
>> I read in some tabular data using this:
>>
>>> read.table(file, quote = "\"", header = TRUE, sep = "\t",
>>>       stringsAsFactors > = FALSE, comment.char = "")
>>
>> One slight problem I have now is that some columns in the data set contain
>> either "" or "+", which means FALSE or TRUE respectively. The command
>> results in those columns being numeric with "NA" corresponding to the empty
>> case and "0" to the "+"-case. Is there any smart way of making read table
>> rendering these columns boolean to start with?
>>
>> Thanks for any insight, Joh
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list