I want to read a file with read.csv2 function. This file contains blank spaces in column names. Whith the parameter header = FALSE I can read the file but when I replace FALSE by TRUE, I have this error :
Error in make.names(col.names, unique = TRUE) : chaîne de charactères multioctets incorrecte 7
How can I manage this error?
My code :
client <- read.csv2("./data/Clients.csv", header = T, na.strings = "",
stringsAsFactors = FALSE, sep = ";", encoding = "UTF-8")
Thanks for your help.
The reason for the error is pointing towards column name having a multi-bytes character which is not compatible with UTF-8.
An option is to use encoding = "UCS-2LE":
client <- read.csv2("./data/Clients.csv", header = TRUE, na.strings = "",
stringsAsFactors = FALSE, sep = ";", encoding = "UCS-2LE")
Related
I'm trying to import a csv file with a data frame:
pc2020 <- read.table("pc2020.csv", sep = ";", header = TRUE)
This works ok, but the enconding is wrong, thus, I get all messed up accentuated characters.
So, I'm trying with:
pc2020 <- read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8")
That returns:
Error in read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8") :
no lines available in input
In addition: Warning message:
In read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8") :
invalid input found on input connection 'pc2020.csv'
You can use read.csv() function with the same attributes you used with read.table. Except fileEncoding - in read.csv() you should write just encoding = "UTF-8".
Also Duck's answer is suitable, too.
I have this code:
write.table(df, file = f, append = F, quote = TRUE, sep = ";",
eol = "\n", na = "", dec = ".", row.names = FALSE,
col.names = TRUE, qmethod = c("escape", "double"))
where df is my data frame and f is a .csv file name.
The problem is that the resulting csv file has an empty line at the end.
When I try to read the file:
dd<-read.table(f,fileEncoding = "UTF-8",sep = ";",header = T,quote = "\"")
I get the following error:
incomplete final line found by readTableHeader
Is there something I am missing?
Thank you in advance.
UPDATE: I solved the problem deleting the UTF-8 file encoding into the read.table:
dd<-read.table(f,sep = ";",header = T,quote = "\"")
but I can't explain the reason of this, since the default for write.table seems to be UTF-8 anyway (I checked this using an advanced text tool).
Any idea of why this is happening?
Thank you,
I have a .csv file tab delimited. While running the code
data <- read.table("xxx.csv",sep = "\t", dec=".", header = TRUE,
encoding="UTF-8", stringsAsFactors = FALSE)
R reads it as a single column without dividing (should make 42 columns). Any ideas? Link to file.
The problem arises because each line is between quotation marks (the whole line).
There are two possible ways to read the file.
Keep all quotation marks.
Use the parameter quote = "" to disable quoting.
read.table("xxx.csv", sep = "\t", dec = ".", header = TRUE,
encoding = "UTF-8", stringsAsFactors = FALSE, quote = "")
Remove the quotation marks before reading the file.
tmp <- gsub('^\"|\"$', '', readLines("xxx.csv"))
read.table(text = tmp, sep = "\t", dec = ".", header = TRUE,
encoding = "UTF-8", stringsAsFactors = FALSE)
I want to create a function that changes specific cells of a given CSV file(s) and saves them back into the exact same format.
I have tried to read the CSV file and write it back with the edited values, but the only way to do it without misinterpreting commas is to quote everything.
Is there a way to edit the files without putting quotes for every single value?
My guess is that there is some other way to read and write the CSV files than read.table
p.s. The csv files I want to edit are not in values seperated by columns, but have rather vague format(there may be unquoted, quoted (mostly because of a comma in a string) strings and integers in the same column/row)
Here is the code I use:
editcell = function(id,
newvalue,
row,
col,
dbpath = "C:/data/",
add = FALSE
)
{
#READ
temptable = read.table(file = file(paste0(dbpath, id, ".csv")),
header = F,
sep = ",",
dec = ".",
fill = TRUE,
quote = "",
comment.char = "",
colClasses = "character",
# nrows = 400,
stringsAsFactors = F
);
cat("There are",nrow(temptable),"rows and",
ncol(temptable),"columns.");
#EDIT
if(add == TRUE) { temptable[x,y] = paste0(temptable[x,y],newvalue)}
else {temptable[x,y] = newvalue};
#WRITE
write.table(temptable,
file=paste0(dbpath, id,".csv"),
append = F,
quote = T,
sep=",",
eol = "\n",
na = "NA",
dec = ".",
row.names=F,
col.names=F,
qmethod = "double",
fileEncoding = ""
)
}
I'm loading a csv file into R from the Quandl database.
the file is comma delimited and the data looks as follows:
quandl code,name
WIKI/ACT,"Actavis, Inc."
WIKI/ADM,"Archer-Daniels-Midland Company"
WIKI/AEE,"Ameren Corporation"
...
...
i use the following code to load the data:
US.Stocks <-read.table(file=ABC,header=FALSE,sep=",")
however, i get the following error:
Error in read.table(data.frame(file = ABC, header = FALSE, :
'file' must be a character string or connection
Can someone pls help me with what im doing wrong? suspect ive not classified some parameter in the read.csv command?
thanks
Tom
you should use
read.csv(file = yourFilePath, header = TRUE)
but I think the problem is in your file path, maybe you are missing file extension and remember to wrap your file path in double qoutes ( "yourfilepath" )
UPDATE:
read.csv is just wrappers around read.table with some default parameters
function (file, header = TRUE, sep = ",", quote = "\"", dec = ".",
fill = TRUE, comment.char = "", ...) {
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)
}