Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify path in write.csv function

Tags:

r

I have a simple syntax question: Is there a way to specify the path in which to write a csv file within the .csv function itself?

I always do the following:

setwd("C:/Users/user/Desktop")
write.csv(dt, "my_file.csv", row.names = F)

However, I would like to skip the setwd() line and include it directly in the write.csv() function. I can't find a path setting in the write.csv documentation file. Is it possible to do this exclusively in write.csv without using write.table() or having to download any packages?

I am writing around 300 .csv files in a script that runs auomatically everyday. The loop runs slower when using write.table() than when using write.csv(). The whole reason I want to include the path in the write.csv() function is to see if I can decrease the time it takes to execute any further.

like image 686
Arturo Sbr Avatar asked Oct 31 '18 17:10

Arturo Sbr


People also ask

How to write to CSV file in Python?

To write to csv file write.csv () function is used. Let us first create a data frame. Now let us write this data to a csv file and save it to a required location.

What are the options passed to write a CSV file?

Other options passed to write.csv2 or write.csv. AlphaPart, object returned from AlphaPart function or summaryAlphaPart, object returned from summary.AlphaPart function. Character, file name with or without .csv extension, e.g., both "file" and "file.csv" are valid.

How to write data to CSV file using R programming language?

So, In this article we are going to learn that how to write data to CSV File using R Programming Language. To write to csv file write.csv () function is used. Let us first create a data frame. Now let us write this data to a csv file and save it to a required location.

What is a CSV file and how to open it?

The CSV file (Comma Separated Values file) is a widely supported file format used to store tabular data. It uses commas to separate the different values in a line, where each line is a row of data.


2 Answers

I typically set my "out" path in the beginning and then just use paste() to create the full filename to save to.

path_out = 'C:\\Users\\user\\Desktop\\'
fileName = paste(path_out, 'my_file.csv',sep = '')
write.csv(dt,fileName)

or all within write.csv()

path_out = 'C:\\Users\\user\\Desktop\\'
write.csv(dt,paste(path_out,'my_file.csv',sep = ''))
like image 180
selene Avatar answered Jan 18 '23 02:01

selene


There is a specialized function for this: file.path:

path <- "C:/Users/user/Desktop"
write.csv(dt, file.path(path, "my_file.csv"), row.names=FALSE)

Quoting from ?file.path, its purpose is:

Construct the path to a file from components in a platform-independent way.

Some of the few things it does automatically (and paste doesn't):

  • Using a platform-specific path separator
  • Adding the path separator between path and filename (if it's not already there)
like image 39
lebatsnok Avatar answered Jan 18 '23 00:01

lebatsnok