admin管理员组

文章数量:1278910

Is there any way to read data in a file and automatically convert columns that contains only "True" and "False" in logical ? It works for "T/F" or uppercase "TRUE/FALSE", but not "True/False", which is unfortunately the way Python write it.

Is there any way to read data in a file and automatically convert columns that contains only "True" and "False" in logical ? It works for "T/F" or uppercase "TRUE/FALSE", but not "True/False", which is unfortunately the way Python write it.

Share Improve this question edited Feb 28 at 18:09 jpsmith 17.7k6 gold badges22 silver badges45 bronze badges asked Feb 24 at 15:52 KiffikiffeKiffikiffe 937 bronze badges 3
  • What function are you using to import the data? Most likely you'll just need to read it in as a string and then convert to boolean with an ifelse() or case_when(). Or if you have a value like x <- c("True", "False", "True") you could convert to upper case and use the default parsing: type.convert(toupper(x)) – MrFlick Commented Feb 24 at 15:56
  • What about as.logical(c("True", "False", "True"))? – Tim G Commented Feb 24 at 16:03
  • 1 Would be good to see a snippet of the data to read-in/already read in. – Friede Commented Feb 24 at 18:15
Add a comment  | 

3 Answers 3

Reset to default 6

Just coerce as.logical.

> x <- c("T", "False", "FALSE", "False", "True", "true", "TRUE", 'foo')
> as.logical(x)
[1]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE    NA

Just use data.table::fread.

library(data.table)
fread("x
      True
      False")
#        x
#   <lgcl>
#1:   TRUE
#2:  FALSE

While as.logical(x) works great to convert strings to boolean, this does not answer the part of your question that asked for automatic detection of columns that only contain "True" or "False". For this, you can use a tidyverse approach to only coerce columns which contain "True" or "False" to boolean

dat <- data.frame(var1=sample(c("A","B"), size=10, replace=T), 
                  var2=sample(c("True","False"),10, replace = T))

   var1  var2
1     A False
2     A False
3     A False
4     B  True
5     A False
6     B  True
7     B False
8     B  True
9     A  True
10    A  True

library(dplyr)
dat1 <- dat %>%
  mutate(across(where(~ all(. %in% c("True", "False"))), ~ . == "True"))

> dat1
   var1  var2
1     A FALSE
2     A FALSE
3     A FALSE
4     B  TRUE
5     A FALSE
6     B  TRUE
7     B FALSE
8     B  TRUE
9     A  TRUE
10    A  TRUE

Or do it in base R without using "Return" ;)

dat2 <- as.data.frame(lapply(dat, function(col) 
  if (all(col %in% c("True", "False"))) as.logical(col == "True") else col
))

Which approach is best depends on your data structure as @MrFlick suggested

本文标签: rAutomatically read a column of lowercases True and False as logicalStack Overflow