简体   繁体   中英

R changing decimals to incorrect scientific notation

I'm trying to import data from an excel spreadsheet into R in this format:

Sample Test
A 1
B 0.006
C 0.235668
D 0.000004
E 4
F >64

However when I load it into R using read_excel it looks like this:

Sample Test
A 1
B 6.0000000000000001E-3
C 0.235668
D 3.9999999999999998E-6
E 4
F >64

I've tried formatting it in excel as general or text (can't do numeric because of some symbols) and I've tried options(scipen=100) and format(test$test, scientific = FALSE, justified = "none") and no matter what I it still formats it in this way. WHY???

I created an Excel sheet using your data. It seems that the issue is the sixth row where the value for Test = ">64". This means that column Test in the resulting data frame is of type character.

However, I only see scientific notation for the value 0.000004, which becomes 4.0E-6, so I suspect that your example data is not exactly what you have in your Excel file.

One solution may be to use dplyr::mutate to convert the column to numeric. Assuming the data frame is named test :

test <- test %>% 
  mutate(Test = as.numeric(gsub(">", "", Test)))

If you have symbols other than ">" (you mention "some symbols"), you'll need to modify the gsub .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM