Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

read.table

I have a file with the data c("A","T","B","F").

When I use:

read.csv(myfile,header=F,stringsAsFactors=F)

R interprets character T as TRUE and F as FALSE

Am I doing anything wrong?

like image 765
nopeva Avatar asked Apr 25 '13 12:04

nopeva


People also ask

What option is used to specify the delimiter in the read table () function in R?

table() function reads a file into data frame in table format. The file can be comma delimited or tab or any other delimiter specified by parameter "sep=".

Why are headers true in R?

a logical value indicating whether the file contains the names of the variables as its first line. If missing, the value is determined from the file format: header is set to TRUE if and only if the first row contains one fewer field than the number of columns. the field separator character.

What does header false mean in R?

This means that the first row of values in the .csv is set as header information (column names). If your data set does not have a header, set the header argument to FALSE : # The first row of the data without setting the header argument: carSpeeds[1, ]


2 Answers

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", ...)

like image 61
Arun Avatar answered Sep 21 '22 12:09

Arun


I came across to similar problem here is the solution:

#dummy data
df <- read.csv(text="
A,B,T,T,F
T,T,F,T,text1
A,T,NA,F,T",
               header=FALSE, stringsAsFactors=FALSE)
#data
df
#   V1 V2    V3    V4    V5
# 1  A  B  TRUE  TRUE     F
# 2  T  T FALSE  TRUE text1
# 3  A  T    NA FALSE     T

#convert logical columns to single letters
df[,sapply(df,class) == "logical"] <-
  sapply(df[,sapply(df,class) == "logical"],
         function(i) substr(as.character(i),1,1))

#result
df
#   V1 V2   V3 V4    V5
# 1  A  B    T  T     F
# 2  T  T    F  T text1
# 3  A  T <NA>  F     T
like image 41
zx8754 Avatar answered Sep 20 '22 12:09

zx8754