export df as csv in r

To export a dataframe as a csv file in R, you can use the write.csv() function. Here's an example:

main.r
# create a dataframe
df <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(25, 30, 35),
  city = c("New York", "Los Angeles", "Chicago")
)

# export the dataframe as a csv file
write.csv(df, "my_dataframe.csv")
224 chars
10 lines

In this example, we create a dataframe df with three columns (name, age, and city), and three rows of data. Then, we use the write.csv() function to export this dataframe as a csv file named my_dataframe.csv.

Note that the csv file will be saved in your current working directory (you can check your current working directory using the getwd() function). If you want to specify a different directory or file path for the saved csv file, you can include that as the second argument to write.csv().

gistlibby LogSnag