英文:
Trying to check if a column name is equal to a certain string
问题
可能是一个简单的答案,但我遇到了麻烦。
为什么会是这种情况?
colnames(handx[[x]][2])
[1] "Player vs. L"
> colnames(handx[[x]][2]) == "Player vs. L"
[1] FALSE
谢谢
英文:
Probably a simple answer, but I'm having trouble.
Why is the following the case?
colnames(handx[[x]][2])
[1] "Player vs. L"
> colnames(handx[[x]][2]) == "Player vs. L"
[1] FALSE
Thanks
答案1
得分: 1
The issue you're encountering is likely due to hidden characters or whitespace in the column name. To diagnose the issue, you can print the column name with surrounding quotes to see if there are any hidden characters:
cat('', colnames(handx[[x]][2]), '', sep = '')
If you see any extra characters or whitespace, you can remove them using trimws()
or gsub()
functions:
clean_colname <- trimws(colnames(handx[[x]][2]))
clean_colname <- gsub("\\s+", " ", clean_colname) # Replace multiple spaces with a single space
Then, you can compare the cleaned column name with the target string:
clean_colname == "Player vs. L"
If this still returns FALSE
, there might be some other hidden characters that need to be removed. You can use the gsub()
function to remove specific characters if needed.
英文:
The issue you're encountering is likely due to hidden characters or whitespace in the column name. To diagnose the issue, you can print the column name with surrounding quotes to see if there are any hidden characters:
cat("'", colnames(handx[[x]][2]), "'", sep = "")
If you see any extra characters or whitespace, you can remove them using trimws()
or gsub()
functions:
clean_colname <- trimws(colnames(handx[[x]][2]))
clean_colname <- gsub("\\s+", " ", clean_colname) # Replace multiple spaces with a single space
Then, you can compare the cleaned column name with the target string:
clean_colname == "Player vs. L"
If this still returns FALSE
, there might be some other hidden characters that need to be removed. You can use the gsub()
function to remove specific characters if needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论