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 |3 Answers
Reset to default 6Just 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
版权声明:本文标题:r - Automatically read a column of lowercases True and False as logical - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741255403a2366575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ifelse()
orcase_when()
. Or if you have a value likex <- 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:56as.logical(c("True", "False", "True"))
? – Tim G Commented Feb 24 at 16:03