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

不打扰是莪最后的温柔 提交于 2020-01-21 04:04:28

问题


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?


回答1:


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




回答2:


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



回答3:


If you don't want to change the class of all the columns, revalue works too, but is better for making a simple change to one column.

df$V3 <- as.factor(revalue(df$V3, c("TRUE" = "T", "FALSE" = "F")))


来源:https://stackoverflow.com/questions/16214471/read-table-reads-t-as-true-and-f-as-false-how-to-avoid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!