我们所能做的就是猜测您的问题的根源可能是什么。
这是我的最佳猜测:您的“filter_name”列中包含空格,因此在您去除空格之前,您实际上不应该寻找“Brand”。
如果我的猜测正确,这是一个重现您的问题的最小示例:
首先,一些示例数据:
mydf <- data.frame(Param = c(" Brand ", "Operating System",
"Type ", " Brand ", "Type ",
"Type ", " Brand ", "Type ",
" Brand "), Value = 1:9)
unique(mydf[["Param"]])
# [1] Brand Operating System Type
# Levels: Brand Operating System Type
subset(mydf, Param == "Brand")
# [1] Param Value
# <0 rows> (or 0-length row.names)
print
与参数一起使用quote = TRUE
以查看您的空格data.frame
:
print(mydf, quote = TRUE)
# Param Value
# 1 " Brand " "1"
# 2 "Operating System" "2"
# 3 "Type " "3"
# 4 " Brand " "4"
# 5 "Type " "5"
# 6 "Type " "6"
# 7 " Brand " "7"
# 8 "Type " "8"
# 9 " Brand " "9"
如果这恰好是您的问题,那么gsub
应该快速解决它:
mydf$Param <- gsub("^\\s+|\\s+$", "", mydf$Param)
unique(mydf[["Param"]])
# [1] "Brand" "Operating System" "Type"
subset(mydf, Param == "Brand")
# Param Value
# 1 Brand 1
# 4 Brand 4
# 7 Brand 7
# 9 Brand 9
您可能还想查看strip.white
默认read.table
为FALSE
. 尝试重新读取您的数据,strip.white = TRUE
然后尝试您的子集。