我正在使用它:
NewDf <- sqldf("select * from OldDf where Test = 'TRUE'")
但我在 NewDf 中获得零行。 OldDf 中确实存在 TRUE 的行( Test 列符合逻辑,值为: TRUE 且 FALSE )。
有什么想法吗?
P.S。:这有效:
NewDf <- subset(OldDf, Test != 'FALSE')
答案 0 :(得分:2)
sqldf 将逻辑存储为0
和1
整数,请参阅此示例:
# dummy data
OldDf <- head(mtcars[, 1:2])
OldDf$Test <- c(TRUE, FALSE)
OldDf
# mpg cyl Test
# Mazda RX4 21.0 6 TRUE
# Mazda RX4 Wag 21.0 6 FALSE
# Datsun 710 22.8 4 TRUE
# Hornet 4 Drive 21.4 6 FALSE
# Hornet Sportabout 18.7 8 TRUE
# Valiant 18.1 6 FALSE
library(sqldf)
sqldf("select * from OldDf where Test = 1")
# mpg cyl Test
# 1 21.0 6 TRUE
# 2 22.8 4 TRUE
# 3 18.7 8 TRUE
# This also works, to filter on TRUE
sqldf("select * from OldDf where Test")
# mpg cyl Test
# 1 21.0 6 TRUE
# 2 22.8 4 TRUE
# 3 18.7 8 TRUE
# To get FALSE
sqldf("select * from OldDf where NOT Test")
# mpg cyl Test
# 1 21.0 6 FALSE
# 2 21.4 6 FALSE
# 3 18.1 6 FALSE