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 updatingfns
. And orders inwrite.table
seem to be wrong. First comes the table, which would bedado1
and then the namefns[1]
, which is a string.– Jorge Mendes
In the
sapply
reads the files and thenfor
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"
?– Rui Barradas