0

I am importing data from many .xlsx files to R. The problem is that in R the decimal numbers of the variables disappear, and it just shows the integer digits. Thanks for the help!

Example in .xlsx:

date weight location
2022-01-01 00:00 30.12kg xxxx

in R :

date weight location
2022-01-01 00:00 30 xxxx

How can I solve the problem?

Following my code

library(tidyverse)

dt <- list.files(path = "C:/Users/gim24gj/Documents/scales2022/data/raw/",
                 pattern = ".xlsx",
                 full.names = TRUE) %>%
  map_df(function(x) { 
    x %>% 
      read_xlsx(sheet = "5-minutely",
                cell_cols(c("A:B")),
                col_types = c("date","text")) %>% 
      select(time = 1, weight = 2) %>%
      mutate(id = paste(x)) %>% 
      mutate(id = str_extract(id, pattern = "([^\\/]+$)"),
             weight = as.numeric(str_extract(weight, "(\\d)+"))) %>% 
      select(id, time, weight) 
      
  })
  • 1
    The problem is your regex to extract the numeric from weight. See how `str_extract("30.12kg", "(\\d)+")` only returns 30, not 30.12. –  Aug 26 '22 at 12:44
  • 1
    Welcome to SO! Try `as.numeric(gsub('[a-z]','',weights))`, (it's in base R) to get the weight as numeric (I suppose you need them as numeric). Example: `as.numeric(gsub('[a-z]','','30.12kg'))`. If the problem is not the one @Adam pointed out, you need to share a [MRE](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – s__ Aug 26 '22 at 13:12

0 Answers0