Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Make identical two strings that look the same but are not identical

I have two strings that look the same but are not identical.

> t
[1] "2009_Manaus_Aerotáxi_crash"
> t2
[1] "2009_Manaus_Aerotáxi_crash"
> identical(t,t2)
[1] FALSE
> str(t)
 chr "2009_Manaus_Aerotaxi_crash""| __truncated__
> str(t2)
 chr "2009_Manaus_Aerotáxi_crash"

How can I force these two strings to be equal?

Thanks

like image 801
ruthy_gg Avatar asked Feb 11 '16 12:02

ruthy_gg


Video Answer


1 Answers

If you write your data out to csv and then open the file in a program like Notepad++, then turn on view>all characters you will be able to see if there are something at the ends of your strings like LF or \r or \n. Then you will have a better idea what you need to remove and can use the above advice (stringi::str_cmp()) to test an example of a string that you know was not working to be sure you fixed it. For me the issue turned out to be spaces and this solved my problem:

own_dept_expect %>% mutate(check_field = stringr::str_replace_all(check_field,"[:space:]"," ")) %>% write_csv("C:/Users/me/Desktop/spaces_suck.csv")

I verified in Notepad++ that it was all uniform now.

like image 157
cluelessgumshoe Avatar answered Oct 13 '22 22:10

cluelessgumshoe