Search code examples
rdata.tablefreadsapplyrbindlist

decimal separator in multiple .txt importing


I need to import a multiple .txt files with "." decimal separators in some columns

when i import the data, the numeric variables columns (with decimals like: 16,500.56) are loaded like character variables, using the next code:

library("data.table")
setwd("G:/Mi unidad/R MODELOS/MUTUAL BASE/")
files <-list.files()
DT <- rbindlist(sapply(files, fread, simplify = FALSE), use.names = FALSE)

how can i specify the decimal separator in this formula????


Solution

  • You can read the data set as it is and then perform string replacement to remove the commas. Then convert the vector to numeric. Say DT$x is your vector:

    DT$x <- c("16,500.56","1,236.34","6,936.33")
    DT$x
    [1] "16,500.56" "1,236.34"  "6,936.33"
    DT$x <- gsub(",", "", DT$x)
    DT$x
    [1] "16500.56" "1236.34"  "6936.33" 
    DT$x <- as.numeric(DT$x)
    DT$x
    [1] 16500.56  1236.34  6936.33