How do I convert a .csv file to .tab in R? - r

I need to convert a file of .csv format to one of .tab format. How do I go about this in R?

Copying the correct answer from the comments:
write.table(read.csv("old-file.csv", sep=","), "new-file.tab")
Note that read.csv and write.csv are just read.table and write.table with different defaults.
Also as mentioned in the comments, you might find read_csv and write_tsv from the readr package more pleasant to work with (the built-in R functions can have some surprising behavior).

Related

Is there a way to read in a large document as a data.frame in R?

I'm trying to use ggplot2 on a large data set stored into a csv file. I used to read it with excel.
I don't know how to convert this data into a data.frame. In particular, I have a date column that has the following format: "2020/04/12:12:00". How can I get R to understand this format ?
If it's a csv, you can use:
fread function from data.table. This will be the fastest way to read your csv.
read_csv or read_csv2 (for ; delimited documents) in readr package
If it's .xls (or .xlsx) document, have a look at the readxl package.
All these functions import your data as data.frames (with additional classes like data.table for fread or tibble for read_csv).
Edit
Given your comment, it looks like your file is not an excel but a csv. If you want to convert a column type to date, assuming your dataframe is called df
df[, dates := as.POSIXct(get(colnames(df)[1]), format = "%Y/%m/%d:%H:%M")]
Note that you don't need to use cbind or even reassign the data.table because you use := operator
As the message is saying you, you don't need the extra-precision of POSIXlt
Going by the question alone, I would suggest the openxlsx package, it has helped me reduce the time significantly in reading large datasets. Three points you may find it to be helpful based on your question and the comments
The read command stays same as xlsx package, however would suggest you to use openxlsx::read.xslx(file_path)
the arguments are again same, but in the place of sheetIndex it is sheet and it takes only numbers
If the existing columns are converted to character, then a simple as.Date would work

Reading in file gives empty rows and columns

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?

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)

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.

Importing data into R, which fileformat is the easiest?

I have a few datasets in the following formats: .asc, .wf1, .xls. I am indifferent about which one I use, as they are exactly the same. Could anyone please tell me which one of the fileformats is easiest to import into R, and how this is done?
save xls to txt or csv, they are easiest for R to read:
but be sure that only one header line or no header line is recommended
try
read.table('*.txt', header=T)
read.table('*.txt', header=F)
read.delim(*, header=F)
read.csv("*.csv")
etc.
Definitely not .xls. If .asc is some sort of fixed-width format, than that can be read in easily with read.csv or read.table.
Other formats that are easy to read include CSV (comma- or tab-separated text files) and DTA (Stata files, via read.dta in the foreign package).
Edit: #KarlOveHufthammer pointed out that .asc is most likely a fixed-width format. In which case read.fwf is the tool to use to read it in to R. Note that FWF is a pain in the heiny to deal with, though, in that you have to have the column widths and names of every column stored somewhere else, then convert that to a format that read.fwf can use--and that's before problems like overlapping ranges.

Resources