Problem while uploading and converting.txt files

Asked

Viewed 39 times

0

Every time I try to do the operation below I get a mistake:

setwd('C:\\Users\\MatheuS\\Documents\\file')

library(readr)


fns = list.files(patt="\\.txt")

sapply(fns, function(x){ assign(gsub("C:\\Users\\MatheusdosSantos\\Documents\\files\\.txt","", x), read.table(x, head=T,sep=";"),
                                envir=.GlobalEnv); NULL})

n <- length(fns)


for (i in 1:n){
  dado1[i] <- read.table(fns[i], head=T,sep=";")
  names(fns[i])[grep("ï..ID", names(fns[i]))] <- "ID"
  write.table(fns[i], dado1, sep="\t", row.names = FALSE, quote=FALSE, na= "", eol = "\r\n")
}


dado1 <- read.table(fns[i], head=T,sep=";")

This operation goes up (Upa) a arquivo.txt separated by ; and exports it with the same name overwriting the old file separating it now by \t (tabulation) and change the name.

But it always makes that mistake:

Error in isOpen(file, "w") : invalid connection
In addition: Warning messages:
1: In `[<-.data.frame`(`*tmp*`, i, value = list(ï..ID = 1:29998, NR_CONTA = c(10000063L,  :
  provided 53 variables to replace 1 variables
2: In if (file == "") file <- stdout() else if (is.character(file)) { :
  the condition has length > 1 and only the first element will be used
  • Look, just a few questions first. The sapply Is that right? Because he is not being associated with any variable, he is not updating fns. And orders in write.table seem to be wrong. First comes the table, which would be dado1 and then the name fns[1], which is a string.

  • In the sapply reads the files and then for re-read them, why? If you only want to change the column separator and the name of one of the columns (from "ï..ID" for "ID"), is complicating a lot. And by the way, you can explain why to change the ";" for "\t"?

1 answer

1

The code of this answer has been tested with files that match what we know by the description in the question, it is not guaranteed to work without errors or adaptation.

First, if you are going to change directory, get the current one and then reset it if necessary.

old_dir <- getwd()
setwd('~/Documentos')

Now processing. All files are read in one much simpler instruction, the read.csv2 you already have sep = ";" and header = TRUE as values of the respective arguments to be passed to the more general function read.table.

fns <- list.files(pattern = '\\.txt')

df_list <- lapply(fns, read.csv2)
df_list <- lapply(df_list, function(DF){
  i <- grep('\\.\\.ID', names(DF), ignore.case = TRUE)
  names(DF)[i] <- 'ID'
  DF
})
lapply(seq_along(df_list), function(i){
  write.table(df_list[[i]], fns[i], row.names = FALSE, sep = '\t', quote = FALSE, na = '')
})

This code can be further simplified by running only one cycle lapply that does everything and has as output the same list df_list with all tables.

fns <- list.files(pattern = '\\.txt')

df_list <- lapply(fns, function(x){
  DF <- read.csv2(x)
  i <- grep('\\.\\.ID', names(DF), ignore.case = TRUE)
  names(DF)[i] <- 'ID'
  write.table(DF, x, row.names = FALSE, sep = '\t', quote = FALSE, na = '')
  DF
})

And in the end restore the initial directory, if necessary, as said above.

setwd(old_dir)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.