Export CSV in R Using write.csv() Function

To export data frame to csv in R, you can use the “write.csv()” function. The write.csv() function “writes data into a CSV file”. It takes a data frame as data, a filepath as a file, and exports it to that file.

Syntax

write.csv(x, file, traitsAsDir = FALSE, csv2 = TRUE, 
          row.names = FALSE, fileEncoding = "UTF-8" ...)

Parameters

  1. x: It is a data frame or other R data added to the csv.
  2. file: It is a character file name with or without a .csv extension, e.g., both “file” and “file.csv” are valid.
  3. traitsAsDir: Logically, the result should be saved within trait folders; the construction is a file.path(dirname(), trait, basename()); folders are created if they do not exist.
  4. csv2: It is a Logical export using write.csv2 or write.csv.
  5. row.names: It is a Logical export row name as well.

How to Export DataFrame to CSV in R

df <- data.frame(
  service_id = c(1:5),
  service_name = c("Netflix", "Disney+", "HBOMAX", "Hulu", "Peacock"),
  service_price = c(18, 10, 15, 7, 12),
  stringsAsFactors = FALSE
)

write.csv(df, "shows.csv", row.names = FALSE)
cat("The data frame is exported", "\n")

Output

 The data frame is exported

And you can see in your current working directory that a file will be created called shows.csv.

Export a CSV file without an index or row number in R

To export a CSV file without an index or row number in R, you must set the “row.names” argument to FALSE in the write.csv() function.

df <- data.frame(
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(22, 15, 19),
  Vote = c(TRUE, FALSE, TRUE)
)

write.csv(df, file = "new_file.csv", row.names = FALSE)

Output

The newly created new_file.csv file has the following contents.

"Name","Age","Vote"
"Juan",22,TRUE
"Alcaraz",15,FALSE
"Simantha",19,TRUE

Export a CSV file in which NA is replaced as an empty string

To write a CSV file with NA values as blank or empty strings in R, you must set the na argument to “” in the write.csv() function.

df <- data.frame(
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(22, 15, NA),
  Vote = c(TRUE, NA, TRUE)
)

write.csv(df, file = "new_file.csv", na = "", row.names = FALSE)

Output

"Name","Age","Vote"
"Juan",22,TRUE
"Alcaraz",15,
"Simantha",,TRUE

You can see that we replaced NA values with “”(empty strings).

Export a CSV file without quotes

To export a CSV file without quotes in R, you must set the “quote” parameter to FALSE in the write.csv() function.

df <- data.frame(
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(21, 19, 46),
  Vote = c(TRUE, FALSE, TRUE)
)

write.csv(df, file = "new_file.csv", row.names = FALSE, quote = FALSE)

Output

Name,Age,Vote
Juan,21,TRUE
Alcaraz,19,FALSE
Simantha,46,TRUE

Export a CSV file with encoding

To write a CSV file with a specific encoding in R, you must set the “fileEncoding” parameter to the desired encoding name in the write.csv() function.

df <- data.frame(
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(21, 19, 46),
  Vote = c(TRUE, FALSE, TRUE)
)

write.csv(df, file = "new_file.csv", row.names = FALSE, fileEncoding = "UTF-8")

Output

Name,Age,Vote
Juan,21,TRUE
Alcaraz,19,FALSE
Simantha,46,TRUE

Export CSV without Header using write.table()

To export a CSV file without a header using write.table() in R, you need to set the “col.names” argument to FALSE in the write.table() function.

df <- data.frame(
  Name = c("Juan", "Alcaraz", "Simantha"),
  Age = c(21, 19, 46),
  Vote = c(TRUE, FALSE, TRUE)
)

write.table(df, file = "new_file.csv", col.names = FALSE)

Output

Check out the new_file.csv file. It has the following contents.

"1" "Juan" 21 TRUE
"2" "Alcaraz" 19 FALSE
"3" "Simantha" 46 TRUE

That’s it.

1 thought on “Export CSV in R Using write.csv() Function”

Leave a Comment