检查列名是否等于特定字符串

huangapple go评论63阅读模式
英文:

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(&quot;&#39;&quot;, colnames(handx[[x]][2]), &quot;&#39;&quot;, sep = &quot;&quot;)

If you see any extra characters or whitespace, you can remove them using trimws() or gsub() functions:

clean_colname &lt;- trimws(colnames(handx[[x]][2]))
clean_colname &lt;- gsub(&quot;\\s+&quot;, &quot; &quot;, clean_colname) # Replace multiple spaces with a single space

Then, you can compare the cleaned column name with the target string:

clean_colname == &quot;Player vs. L&quot;

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.

huangapple
  • 本文由 发表于 2023年4月17日 12:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定