How To Write DataFrame To CSV In R
Let us first create our dataframe. For this exercise, I have downloaded the data from here...
kaggle.com/sudalairajkumar/covid19-in-india/data#
I have unzipped the data and my data lives here data/indiaCovid19/covid_19_india.csv
Ok, now we can read our csv file in R with just simple command read.csv and passing option header=TRUE
df = read.csv('data/indiaCovid19/covid_19_india.csv',header = TRUE)
Let us check the number of rows in our dataframe.
nrow(df)
Let us take a look at the first two rows of our dataframe.
head(df,2)
Ok let us write out dataframe in to csv file using R command write.csv.
write.csv(df,'MyData.csv')
Let us check if our file is present. In R, we can run unix commands by using system command. To print the output at the console, set intern=TRUE
system("ls -lrt MyData.csv",intern = TRUE)
Let us check the first two rows of our dataframe using cat command in R.
system('cat MyData.csv | head -2',intern=TRUE)
Note, we got one extra column including row numbers too in the file. We can disable this by using option row.names=FALSE in write.csv in R.
write.csv(df,'MyData.csv',row.names = FALSE)
Now let us check the first two rows again.
system('cat MyData.csv | head -2',intern=TRUE)
Wrap Up!
That is pretty much about writing dataframe to csv file in R.
Related Notebooks
- How to Export Pandas DataFrame to a CSV File
- How To Read CSV File Using Python PySpark
- How to Analyze the CSV data in Pandas
- How to Create DataFrame in R Using Examples
- How To Iterate Over Rows In A Dataframe In Pandas
- How To Replace na Values with Zeros In R Dataframe
- How To Append Rows With Concat to a Pandas DataFrame
- 3 Ways to Rename Columns in Pandas DataFrame
- Save Pandas DataFrame as CSV file