Troubles with importing data into R - r

I know it is a basic question and I have been looking for a specific answer for months.
Here is the deal:
Every time that I try to import tables to R, there is a problem and they never get imported properly. I have done this with my own files and with files that I got from courses. I have tried putting comas, semicolons, I have used the (header=TRUE,
sep=",", row.names="id")
But it just won't work.
Here is what I mean. I am really getting desperate with being unable to complete this very simple task that prevents me to go on with the actual analysis.
Thank you very much in advance.
enter image description here
enter image description here

Lke the first comment says, the problem is the separator. You can try
fread(file_name, sep = ";") # data.table package
read.csv(file_name, sep = ";") # utils package
It could be that you're importing European .csv files or that your Excel is set to some other language. You may also want to check on the decimal separator dec = "," which is common for European .csv files.

Related

R misreading csv files after modifications on Excel

This is more of a curiosity.
Sometimes I modify csv files from Excel rather than R (suppose I manage to find a missing piece of info and I type it in the csv file), of course maintaining commas and quotes as they were.
Every time I do this, R becomes unable to read the csv file, i.e. it imports a single column as it appears on Excel, rather than separating the values (no options like sep= or quote= change this).
Does anyone know why this happens?
Thanks a lot
An example
This was readable:
state,"city","county"
AK,"Anchorage",""
AK,"Haines",""
AK,"Juneau","Juneau"
After adding the missing info under "county", R fails to import it as a data frame, reading it instead as a single vector.
state,"city","county"
AK,"Anchorage","Anchorage"
AK,"Haines","Haines"
AK,"Juneau","Juneau"
Edit:
I'm just running the basic read.csv
df <- read.csv("C:/directory/df.csv")

When importing data into ChemoSpec, I get: Error in `[.data.frame`(temp, , 2) : undefined columns selected

I'm new to R (and any kind of programming language in general) and was hoping for a package to help analyze some HPLC data. My script:
library(ChemoSpec)
spec <- files2SpectraObject(gr.crit=c("Control","AC","Fifty"),
gr.cols=c("auto"), freq.unit="minutes", int.unit="mAU",
descrip="hplc test data", fileExt=".csv",
out.file="hplc test data", debug=TRUE)
And the output:
The default behavior of this function has changed as of July 2016. See
?files2SpectraObject. Really: please read it!
files2SpectraObject is checking the first file
files2SpectraObject will now import your files Importing file:
AC_3G_L_1_220_trim.csv Error in [.data.frame(temp, , 2) : undefined
columns selected
I've got the ChemoSpec pdf and formatted my files accordingly into two columns, no headers, .csv format. Any suggestions as to what I've missed?
I am the author of ChemoSpec -- sorry for the delay in answering!
You probably need to add sep = "," to your files2SpectraObject call. You may also need to set the header and possibly the decimal marker. The only way to know is to open one of your csv files in a plain text editor and see what it looks like. ChemoSpec now allows a lot of flexibility in the format of the csv file, because it turns of that not all instrument manufacturers feel that csv means "comma separated values". Plus, different countries have different standards for the decimal marker (and your instrument may or may not be set up to reflect typical local standards). This is all detailed in ?files2SpectraObject.
There is also a new version of ChemoSpec on CRAN as of a few days ago.

Can't read special characters like ï or â

I'm having problems importing datasets that contains special characters.
At this moment, I am using the following code to import the data:
alias <- read.csv("C:Documents/alias.csv", na.strings = "", sep = ",", header=TRUE,
colClasses=c(id="character"), fileEncoding="UTF-8")
This works fine until a special character comes up like ï or â, R just stops reading the next rows. I only found complex answers to this problem that I did not understand. I found answers how to convert these special characters after they are imported, but for me, the problem is that it stops importing. I have to find a way so that R keeps importing data without stopping.
The picture shows what I get after importing the data. The next variable is not imported, because it stopped at the character after lego-su.
There are some related topics to my problem like this one: Reading special characters like ÆØÅ into R (Rstudio). I don't know how this local stuff works or how I can integrate this into my problem. Or maybe there is another problem different to that one.

missing lines when read csv file into R

I have trouble with reading csv file into R. The file contains more than 10000 lines, but only 4977 lines are read into R. And there is no missing value in the file. My code below:
mydata = read.csv("12260101.csv", quote = "\"", skipNul = TRUE)
write.csv(mydata, "check.csv")
It's hard to say without seeing the CSV file. You might want to compare rows that aren't being imported with the imported ones.
I would try using the function read_csv() from the package readr or fread() from data.table.
As other posters pointed out, hard to reproduce without an example. I had a similar issue with read.csv but fread worked without any problems. Might be worth a shot to give it a try.

Using R, import data from web

I have just started using R, so this may be a very dumb question. I am trying to import the data using:
emdata=read.csv(file="http://lottery.merseyworld.com/cgi-bin/lottery?days=19&Machine=Z&Ballset=0&order=1&show=1&year=0&display=CSV",header=TRUE)
My problem is that it reads the csv file into a single column ( by the way, the lottery data is simply because it is publicly available to download - using as an exercise to understand what I can and can't do in R), instead of formatting it into however many columns of data there are. Would someone mind helping out, please, even though this is trivial
Hm, that's kind of obnoxious for a page purporting to be in csv format. You can skip the first 5 lines, which will cause R to read (most of) the rest of the file correctly.
emdata=read.csv(file=...., header=TRUE, skip=5)
I got the number of lines to skip by looking at the source. You'll still have to remove the cruft in the middle and end, and then clean up the columns (they'll all be factors because of the embedded text).
It would be much easier to save the page to your hard disk, edit it to remove all the useless bits, then import it.
... to answer your REAL question, yes, you can import data directly from the web. In general, wherever you would read a file, you can substitute a fully qualified URL -- R is smart enough to do the Right Thing[tm]. This specific URL just happens to be particularly messy.
You could read text from the given url, filter out the obnoxious lines and then read the result as CSV like so:
lines <- readLines(url("http://lottery.merseyworld.com/cgi-bin/lottery?days=19&Machine=Z&Ballset=0&order=1&show=1&year=0&display=CSV"))
read.csv(text=lines[grep("([^,]*,){5,}", lines)])
The above regular expression matches any lines containing at least five commas.

Resources