Read.Table Reads "T" as True and "F" as False, How to Avoid

read.table reads T as TRUE and F as FALSE, how to avoid?

If all your columns are characters then try this:

# replace text = . with your filename
read.csv(text="A,B,T,T", header=FALSE, stringsAsFactors=FALSE,
colClasses = c("character"))

Else, you'll have to pass the type of each column in colClasses as: colClasses = c("numeric", "numeric", "character", ...)

read.table unexpectedly interprets T as TRUE

What makes you think this is "unexpectedly" ?

R guesses for you (and that is generally helpful), but if you know better, use the colClasses=... argument to tell R.

R> res <- read.table(textConnection("col1 col2\n1 T\n2 T\n3 T"), 
+ header=TRUE, colClasses=c("numeric", "character"))
R> res
col1 col2
1 1 T
2 2 T
3 3 T
R> sapply(res, class)
col1 col2
"numeric" "character"
R>

Your post was a little oddly formatted so I didn't see at first that you did in fact specify colClasses. Despite the recycling rule I always recommend to supply a vector
with as many entries as you have columns.

Read.csv is treating T as True and F as False, even if I am using stringsAsFactors=FALSE

You can use the colClasses argument to specify the types of each column. That way, you'd be able to tell R that you want a character column during the read.

for example,

raw_time_series_df <- read.csv(input_file_names[i], header=FALSE, as.is=TRUE,
strip.white=TRUE, colClasses="character")

Will return a data.frame where all variables are of type "character." You can specify column types individually by feeding colClasses a vector of types. From the help file, ?read.table, colClasses is

A vector of classes to be assumed for the columns. Recycled as necessary. If named and shorter than required, names are matched to the column names with unspecified values are taken to be NA.

Possible values are NA (the default, when type.convert is used), "NULL" (when the column is skipped), one of the atomic vector classes (logical, integer, numeric, complex, character, raw), or "factor", "Date" or "POSIXct".

Note that "NULL" can be used to skip a column.

T' & 'F' reads as True and False from database

sqlQuery function has as.is option, from the RODBC manual:

as.is which (if any) columns returned as character should be converted to another type? Allowed values are as for read.table. See
‘Details’.

Details
... if as.is is true for a column, it is returned as a character
vector. Otherwise (where detected) date, datetime and timestamp values
are converted to the "Date" or "POSIXct" class. ...

How to avoid R converting F to False when converting logical vector to character

If you know that the problematic columns are first_sex and second_sex, you can use the col_* handlers from readr. For example:

require(readr)    
notlogical<-cols(first_sex=col_character(),second_sex=col_character())
#then in the lapply:
test_data <- lapply(files, read_csv, col_types=notlogical) #the rest is the same

Error in reading a CSV file with read.table()

read.csv is defined as:

function (file, header = TRUE, sep = ",", quote = "\"", dec = ".", 
fill = TRUE, comment.char = "", ...)
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)

You need to add quote="\"" (read.table expects single quotes by default whereas read.csv expects double quotes)

EmpSal <- read.csv('Baltimore_City_Employee_Salaries_FY2015.csv')
EmpSal1 <- read.table('Baltimore_City_Employee_Salaries_FY2015.csv', sep=',', header = TRUE, fill = TRUE, quote="\"")
identical(EmpSal, EmpSal1)
# TRUE

read.table() in R did not read all the columns

You don't have to manually add columns. While reading try the fill=TRUE option :

read.table("mytxt.txt",header=FALSE,fill=TRUE)


Related Topics



Leave a reply



Submit