1

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
sjcasr
  • 11
  • 2
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. What does `charToRaw(colnames(handx[[x]][2]))` show? – MrFlick Apr 17 '23 at 04:51

1 Answers1

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.

rc_tn
  • 57
  • 1
  • 8
  • Thank you for the response! Oddly I'm still having issues though. I'm getting. > cat("'", colnames(handx[[x]][2]), "'", sep = "") 'Player vs. L' > trimws(colnames(handx[[x]][2])) [1] "Player vs. L" > trimws(colnames(handx[[x]][2])) == "Player vs. L" [1] FALSE – sjcasr Apr 17 '23 at 04:38
  • 1
    Hmm.. I'd still think it's non-printable characters. Try: clean_colname <- gsub("[^[:print:]]", "", colnames(handx[[x]][2])) This will remove any non-printable characters from the column name. – rc_tn Apr 17 '23 at 04:49