I have two vectors of filenames, sample_files and actual_files. Each vector contains the filepaths of 4 files. In sample_files, each file is a .csv file and when I call lapply(sample_files, read.csv), I get a list with four dataframes as expected.
However, with the actual_files vector, each file is a .txt file that is double-pipe delimited (blurgh) and I get the below error when calling lapply(actual_files, read.csv(sep="|"))
Error in read.table(file = file, header = header, sep = sep, quote = quote,
: argument "file" is missing, with no default
I tried reading in each file separately with read.csv(filename, sep="|") and it created the dataframe, no problem.
What am I doing wrong with the second lapply call to get that error?
My question is similar to this question but it hasn't been answered.
thank you!
Related
I have encountered this strange xlsx file, which have a lot of empty columns, and when I swipe rightwards in Microsoft Excel, the empty columns kept appearing. I want to read this file using read.csv, but it gives me warning In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls, I am guessing this is because of the empty columns. How can I solve this and read it using read.csv? Thank you
How can I use |(pipe) as a delimiter while writing csv files in R?
When I try writing a data set into a file with write.csv with sep = "|", it ignores the separator and writes the file simply as a comma separated file.
Also write.csv2 also doesn't seem to cover the other variety of characters which could be used as a separator.
Is there a way to use other characters such as ^, $, ~, ¬ or |, as a delimiter while writing a csv file in R.
Thanks.
You have to understand that .csv means "comma-separated value" https://en.wikipedia.org/wiki/Comma-separated_values.
If you want to export with a separator using that characters you need another function.
For example, using write.table, and you'll be able to load this file with R, Excel,....
write.table(data, "data.txt", sep = "|")
data_load <- read.table("data.txt", sep = "|")
Feel free to use any character as separator.
Or you could force this plain text to be .csv
write.table(data, "data.csv", sep = "|")
data_load <- read.csv("data.csv", sep = "|")
This answer is just a variation of the one I gave for this question. They are similar, but I don't think the question itself is an exact duplicate, but they are both part of a bigger question (not yet asked).
In the help for write.table, it states:
write.csv and write.csv2 provide convenience wrappers for writing CSV files.
...
These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file.
Attempts to change append, col.names, sep, dec or qmethod are ignored,
with a warning.
To set sep or another of these parameters you need to use write.table instead of write.csv.
I am having a problem with using read.csv in R. I am trying to import a file that has been saved as a .csv file in Excel. Missing values are blank, but I have a single entry in one column which looks blank, but is in fact a space. Using the standard command that I have been using for similar files produces this error:
raw.data <- read.csv("DATA_FILE.csv", header=TRUE, na.strings="", encoding="latin1")
Error in type.convert(data[[i]], as.is = as.is[i], dec = dec, na.strings = character(0L)) :
invalid multibyte string at ' floo'
I have tried a few variations, adding arguments to the read.csv() command such as na.strings=c(""," ") and strip.white=TRUE, but these result in the exact same error.
It is a similar error to what you get when you use the wrong encoding option, but I am pretty sure this shouldn't be a problem here. I have of course tried manually removing the space (in Excel), and this works, but as I'm trying to write generic code for a Shiny tool, this is not really optimal.
I would like to read a bunch of csvs into a list in R
lol<-lapply(list.files()[c(grep(Sys.Date(),list.files()))],read.csv)
Some csv files are empty though so I get
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
no lines available in input
(at least I think the error means)
How do I read the nonempty csv files into a list?
tryCatch does not work (or I am using it wrong)
tryCatch(toplel<-lapply(list.files()[c(grep(Sys.Date(),list.files()))],read.csv),error=function(e){print("lel")})
[1] "lel"
Try excluding empty files by their size:
files <- list.files()[c(grep(Sys.Date(),list.files()))]
files <- files[which(file.info(files)$size>0)]
lapply(files, read.csv)
This question already has answers here:
'Incomplete final line' warning when trying to read a .csv file into R
(17 answers)
Closed 9 years ago.
I am trying to import CSV files to graph for a project. I'm using R 2.15.2 on a Mac OS X.
The first way tried
The script I'm trying to run to import the CSV file is this:
group4 <- read.csv("XXXX.csv", header=T)
But I keep getting this error message:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
object 'XXXXXX.csv' not found
The second way tried
I tried moving my working directory but got another error saying I can't move my working directory. So I went into Preferences tab and changed the working directory to the file that has my CSV files. But I still get the same error(as the first way).
The third way tried
Then I tried this script:
group4 <- read.table(file.choose(), sep="\t", header=T)
And I get this error:
Warning message:
In read.table(file.choose(), sep = "\t", header = T) :
incomplete final line found by readTableHeader on '/Users/xxxxxx/Documents/Programming/R/xxxxxx/xxxxxx.csv'
I've searched on the R site and all over the Internet, and nothing has got me to the point where I can import this simple CSV file into the R console.
The file is not in your working directory, change it, or use an absolute path.
Than you are pointing to a non-existing directory, or you do not have write privileges there.
The last line of your file is malformed.
As to the missing EOF (i.e. last line in file is corrupted)...
Usually, a data file should end with an empty line. Perhaps check your file if that is the case.
As an alternative, I would suggest to try out readLines(). This function reads each line of your data file into a vector. If you know the format of your input, i.e. the number of columns in the table, you could do this...
number.of.columns <- 5 # the number of columns in your data file
delimiter <- "\t" # this is what separates the values in your data file
lines <- readLines("path/to/your/file.csv", -1L)
values <- unlist(lapply(lines, strsplit, delimiter, fixed=TRUE))
data <- matrix(values, byrow=TRUE, ncol=number.of.columns)