Reading in file gives empty rows and columns - r

Given this CSV file:
How to read a file so that the extra commas that are not a part of data are excluded?

Seems that the file is ok. Have you tried the correct options for arguments in your importing function?
Would you like to try read_delim() from the readr package?

Related

read_csv does not work separate commas and not capture separate rows

I am trying to parse a text log file like this, I can use the default read.csv to parse this file.
test <- read.csv("test.txt", header=FALSE)
It separated all comma parts, though not perfectly put in a dataframe, further manipulation can be done to improve.
However, I can not seem to do so using readr package
test <- read_csv("test.txt", header=FALSE)
All observations turn into 1 row, no separation between commas.
I am learning this package so any help would be great.
{"dev_id":"f8:f0:05:xx:db:xx","data":[{"dist":[7270,7269,7269,7275,7270,7271,7265,7270,7274,7267,7271,7271,7266,7263,7268,7271,7266,7265,7270,7268,7264,7270,7261,7260]},{"temp":0},{"hum":0},{"vin":448}],"time":4485318,"transmit_time":4495658,"version":"1.0"}
{"dev_id":"f8:xx:05:xx:d9:xx","data":[{"dist":[6869,6868,6867,6871,6866,6867,6863,6865,6868,6869,6868,6860,6865,6866,6870,6861,6865,6868,6866,6864,6866,6866,6865,6872]},{"temp":0},{"hum":0},{"vin":449}],"time":4405316,"transmit_time":4413715,"version":"1.0"}
{"dev_id":"xx:f0:05:e8:da:xx","data":[{"dist":[5775,5775,5777,5772,5777,5770,5779,5773,5776,5777,5772,5768,5782,5772,5765,5770,5770,5767,5767,5777,5766,5763,5773,5776]},{"temp":0},{"hum":0},{"vin":447}],"time":4461316,"transmit_time":4473307,"version":"1.0"}
{"dev_id":"xx:f0:xx:e8:xx:0a","data":[{"dist":[4358,4361,4355,4358,4359,4359,4361,4358,4359,4360,4360,4361,4361,4359,4359,4356,4357,4361,4359,4360,4358,4358,4362,4359]},{"temp":0},{"hum":0},{"vin":424}],"time":5190320,"transmit_time":5198748,"version":"1.0"}
Thanks to #Dave2e pointing out that this file is in JSON format, I found the way to parse it using ndjson::stream_in.

Does what='char' works for read.csv function?

The basic format for scan function in R to read a file with characters is represented like this
a<- scan(file.choose(),what='char',sep=',').
I have a csv file with names as a separate column. Can i use what='char' in read.csv. If yes, how to use. If not how to read names column?
There is an entire R manual on importing and exporting data
https://cran.r-project.org/doc/manuals/r-release/R-data.html
read.table (or more specifically read.csv, which is read.table with the default separator being a comma) are the functions you are looking for.
a <- read.csv(yourfile)

Trying to read tweets in R stored in a csv file

I have some tweets stored in a csv file on my local computer.There are 1248 rows. Now when I try to read these tweets in R using the read.csv function I get 1816 rows. This is happening because there are some tweets which have commas in them so basically what read.csv does is it splits one tweet into multiple tweets based on the number of commas and hence more number of rows. So what separator should I define to read the file correctly?
Thanks
Use read.table or read.delim instead of read.csv and use the quote parameter. There's a thread on this that will provide all the details
[read.table with comma separated values and also commas inside each element.
convert csv file to xlsx and use the following code:
library(readxl)
dataset <- read_excel('C:/Study/..._Sample1.xlsx')

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.

Taking manipulated data out of the R console and creating a csv

I have used R to remove duplicates from a csv file using the following (lda_data is my csv file name)
unique(lda_data[duplicated(lda_data),])
This works great, however I need to get the results from the console into another csv file.
What are the methods of getting manipulated data from a csv file into another new and manipulated csv file?
Thanks in advance.
Use the write.csv command:
write.csv(dataframe, "/path/filename.csv", row.names = FALSE)
That should do the trick for you.

Resources