How to write an R vector to a CSV or text file?

Proceed as shown in the example below to export an R vector object into a csv or text file. You can use this code on RStudio, Jupyter Notebook or other R dev environment.

Step 1: Create an R vector

We will define a vector composed from character strings. You could as well define a numeric vector or subset your vector from an existing DataFrame object.

hr_vector <- c('Python', 'R', 'Julia', 'Access', 'Power BI')

Step 2: Define location of your csv file

Next, we will define a path in the file system in which we will save our csv file:

csv_path  = 'hr_vector.csv'

Step 3: Convert your R vector to a text or CSV file

Your next step is to use the write.csv function in order to export the vector into a comma separated file.

write.csv(hr_vector, csv_path, row.names = FALSE)

Step 4: Check your file output

Last, navigate to your file in your Windows, macOS or Linux file system, open it and verify its contents. Here is a screenshot:

Additional Learning

How to remove the first column from your R dataframe object?