Why is R coercing ′ to ' when I try to assign it as a simple character vector but not when I extract it from a list object?

最后都变了- 提交于 2020-07-10 07:31:22

问题


Why is R coercing ′ (the prime symbol) to ' (apostrophe) when I try to assign it as a simple character vector but not when I assign it directly from an list/tibble?

Unfortunately, I can't find a simple way to share the data which is the root of this problem, but please hear me out! It is entirely reproducible if you save a super simple .xlsx file with a single value entered (36°48′31.33):

and hopefully it will become clear in a second why I can't share this in a more efficient manner.

If I read in this file and print it:

library(readxl)
example <- read_xlsx("example.xlsx")
example

#I get this:

# A tibble: 1 x 1
#  latitude   
#  <chr>      
#1 36°48′31.33

which looks all good. However, if I try to type the same thing our as a single character string and assign/print it, I instead get this:

coercedExample <- "36°48′31.33"
coercedExample
#[2] "36°48'31.33"

Which is now printing with a apostrophe (') instead of a the prime symbol (′).

If I use the dput() function then it similarly coerces the value, even if I use it on the tibble that read_xlsx() produces, hence why I could not simple post a copy of the data as read from my PC.

Note this only became an issue since the move to version 4.0.1 for R (what is installed on my machine). I am running readxl version 1.3.1 through RStudio version 1.3.959 on a Windows 10 machine.

This issue arose when I was trying to help @jvan257 with her problem - if you copy and paste her code there is no issue and it runs correctly. However, if you run it from the source .xlsx file it reads in a prime symbol as above and str_replace_all() does not replace the prime symbol when using the call:

library(stringr) #Version 1.4.0
stringr::str_replace_all(string = example, pattern = c("°|'|′|″"), repl=" ")

But it does work for the coerced one and R seems to be recognising the apostrophe as both an apostrophe and a prime symbol as seen here:

str_replace_all(string = coercedExample, pattern = c("°|′|″"), repl=" ") #only replacing ′ in the string  
#"36 48 31.33" #Works
str_replace_all(string = coercedExample, pattern = c("°|'|″"), repl=" ") #only replacing ' in the string
#"36 48 31.33" #works

来源:https://stackoverflow.com/questions/62419815/why-is-r-coercing-to-when-i-try-to-assign-it-as-a-simple-character-vector-bu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!