Hi @FJCC ,
I´ve updated my post because I´ve found that the problem is when I import all my text files. If I work one by one I don´t get any error but when I import my 1728 txt files in a list and try to parse the first column to date format I get NA
in some of them.
This is the code I´m using.
pacman::p_load(knitr,rmdformats,tidyverse,readr, lubridate
#RPostgreSQL,DBI
)
# Indicar ubicación de archivos txt
path <- "C:/Users/Carlos.Cajas/Desktop" # trabajo
paths <- list.files(path=path,
pattern="\\.txt$",
full.names=TRUE)
# Leer lineas archivo
lineas.lst <- lapply(paths, function(path){
read_lines(path)
})
# Buscar cabecera de tabla de datos
header <- lineas.lst[[1]][grepl( "Timestamp" , lineas.lst[[1]])]
header_line <- match(header,lineas.lst[[1]])
# leer archivo
datos.lst <- lapply(paths, function(x){read_delim(x,
"\t", col_types = cols(.default = "c"),
skip = header_line-1)
})
# transformar datos y cambiar comas por puntos
datos.lst <- lapply(datos.lst, function(x){
data.frame(lapply(x, function(x) {
gsub(",", ".", x)
}), stringsAsFactors=FALSE)})
datos.lst <- lapply(datos.lst, function(df) {
df[, -1] <- apply(df[, -1], 2, FUN = function(col) as.numeric(col))
df
})
datos.lst <- lapply(datos.lst, function(df) {
df[,1] <- as.POSIXct(df[,1], format = "%m/%d/%Y %H:%M:%OS")
df
})
datos.df <- bind_rows(datos.lst)
rm(datos.lst)
colnames(datos.df)
#> [1] "timestamp" "CH1Avg" "CH1SD" "CH1Max" "CH1Min" "CH2Avg"
#> [7] "CH2SD" "CH2Max" "CH2Min" "CH3Avg" "CH3SD" "CH3Max"
#> [13] "CH3Min" "CH4Avg" "CH4SD" "CH4Max" "CH4Min" "CH5Avg"
#> [19] "CH5SD" "CH5Max" "CH5Min" "CH6Avg" "CH6SD" "CH6Max"
#> [25] "CH6Min" "CH7Avg" "CH7SD" "CH7Max" "CH7Min" "CH8Avg"
#> [31] "CH8SD" "CH8Max" "CH8Min" "CH9Avg" "CH9SD" "CH9Max"
#> [37] "CH9Min" "CH10Avg" "CH10SD" "CH10Max" "CH10Min" "CH11Avg"
#> [43] "CH11SD" "CH11Max" "CH11Min" "CH12Avg" "CH12SD" "CH12Max"
#> [49] "CH12Min" "CH13Avg" "CH13SD" "CH13Max" "CH13Min" "CH14Avg"
#> [55] "CH14SD" "CH14Max" "CH14Min" "CH15Avg" "CH15SD" "CH15Max"
#> [61] "CH15Min"
Created on 2022-04-22 by the reprex package (v2.0.1)
In this link I´m attaching 2 example files
txt_files
If I run the code for just both files there will be no problem with the timestamp column but when I run over my 1728 files, file_1 has no problem but file_2 gets NA
in the timestamp column.
I hope you can give me some advice.
Thanks.