1

I'm importing a csv into r and there are a-hats (an a with a circumflex / up carat above it) all over the place that don't exist in the raw data.

Does anyone know what they are and how to get rid of them?

Here is the dput(head(df)) result @foc suggested I supply:

 structure(list(V1 = c("", "Race3 and Hispanic Origin", "Whiteâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
"   White, not Hispanicâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
"Blackâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
"Asianâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦"
), V2 = c("", "", "245,985", "195,221", "41,962", "18,879"), 
    V3 = c("", "", "27,113", "17,263", "9,234", "1,908"), V4 = c("", 
    "", "547", "493", "388", "175"), V5 = c("", "", "11.0", "8.8", 
    "22.0", "10.1"), V6 = c("", "", "0.2", "0.3", "0.9", "0.9"
    ), V7 = c("", "", "247,272", "195,256", "42,474", "19,475"
    ), V8 = c("", "", "26,436", "16,993", "8,993", "1,953"), 
    V9 = c("", "", "714", "571", "373", "190"), V10 = c("", "", 
    "10.7", "8.7", "21.2", "10.0"), V11 = c("", "", "0.3", "0.3", 
    "0.9", "1.0"), V12 = c("", "", "-677", "-270", "-241", "45"
    ), V13 = c("", "", "*-0.3", "-0.1", "-0.8", "-0.1")), row.names = c(NA, 
6L), class = "data.frame")
ajbentley
  • 193
  • 1
  • 10
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's probably an encoding problem but hard to say for sure with the limited information provided. What OS are you using? What is the encoding of the file? What is your system locale? – MrFlick Oct 03 '18 at 20:01
  • @ajbently. Please, can you elaborate a little more? Use the `dput(head(df))` and provide some data and explain a little more what you mean with a-hats in order to help you? – foc Oct 03 '18 at 20:08
  • @MrFlick Sorry, I couldn't figure out what I could give as a reproducible example on this one. – ajbentley Oct 03 '18 at 20:17
  • @ajbentley possible you have a problem with utf encoding. Do you use a way like this `df1 <- read.csv("C:/Users/user/Desktop/example.csv", header=TRUE, sep=",", fileEncoding = "UTF-8")` to read your csv? – foc Oct 03 '18 at 20:22
  • @foc More or less. I tried adding the fileEncoding but that returned: Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input – ajbentley Oct 03 '18 at 20:31
  • @ajbentley maybe your csv is separated by ";" and not "," – foc Oct 03 '18 at 20:32
  • @foc nope, they're commas – ajbentley Oct 03 '18 at 20:36
  • It does seem like it could be an encoding issue. I saw `fileEncoding="UTF-8-BOM"` [in this answer](https://stackoverflow.com/a/26347303/2461552) and more general info on encoding [here](https://cran.r-project.org/doc/manuals/r-release/R-data.html#Variations-on-read_002etable). As a work around, have you tried reading it with a different package, like `readr::read_csv`? – aosmith Oct 03 '18 at 21:10

1 Answers1

1

Not sure if this is what you want:

data example:

df <- structure(list(V1 = c("", "Race3 and Hispanic Origin", "Whiteâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
                            "   White, not Hispanicâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
                            "Blackâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦", 
                            "Asianâ\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦â\200¦"
), V2 = c("", "", "245,985", "195,221", "41,962", "18,879"), 
V3 = c("", "", "27,113", "17,263", "9,234", "1,908"), V4 = c("", 
                                                             "", "547", "493", "388", "175"), V5 = c("", "", "11.0", "8.8", 
                                                                                                     "22.0", "10.1"), V6 = c("", "", "0.2", "0.3", "0.9", "0.9"
                                                                                                     ), V7 = c("", "", "247,272", "195,256", "42,474", "19,475"
                                                                                                     ), V8 = c("", "", "26,436", "16,993", "8,993", "1,953"), 
V9 = c("", "", "714", "571", "373", "190"), V10 = c("", "", 
                                                    "10.7", "8.7", "21.2", "10.0"), V11 = c("", "", "0.3", "0.3", 
                                                                                            "0.9", "1.0"), V12 = c("", "", "-677", "-270", "-241", "45"
                                                                                            ), V13 = c("", "", "*-0.3", "-0.1", "-0.8", "-0.1")), row.names = c(NA, 
                                                                                                                                                                6L), class = "data.frame")

remove the character:

df[] <- lapply(df, gsub, pattern='a€¦', replacement='')

results:

df
                         V1      V2     V3  V4   V5  V6      V7     V8  V9  V10 V11  V12   V13
1                                                                                             
2 Race3 and Hispanic Origin                                                                   
3                     White 245,985 27,113 547 11.0 0.2 247,272 26,436 714 10.7 0.3 -677 *-0.3
4       White, not Hispanic 195,221 17,263 493  8.8 0.3 195,256 16,993 571  8.7 0.3 -270  -0.1
5                     Black  41,962  9,234 388 22.0 0.9  42,474  8,993 373 21.2 0.9 -241  -0.8
6                     Asian  18,879  1,908 175 10.1 0.9  19,475  1,953 190 10.0 1.0   45  -0.1
foc
  • 947
  • 1
  • 9
  • 26