29.8 C
London
Saturday, June 28, 2025
HomeRImport & Export Data in RHow to Use the sink() Function in R (With Examples)

How to Use the sink() Function in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the sink() function to drive R output to an external connection.

This function is useful because it lets you easily export character strings or data frames to a CSV file or text file.

This function uses the following basic syntax:

#define file name
sink("my_data.txt")

#write this text to file
"here is some text"

#close the external connection
sink() 

The following examples show three different ways to use this function in practice.

Example 1: Use sink() to Export String to Text File

We can use the following sink() function to export a character string to a text file:

#define file name
sink("my_data.txt")

#write this text to file
"here is some text"

#close the external connection
sink()

We can then navigate to the current working directory and open the text file:

The file contains the string that we specified.

We can also export several character strings to a text file:

#define file name
sink("my_data.txt")

#write several strings to file
"first text"
"second text"
"third text"

#close the external connection
sink()

We can then navigate to the current working directory and open the text file:

The file contains the three strings that we specified.

Example 2: Use sink() to Export Data Frame to Text File

We can use the following sink() function to export a data frame to a text file:

#define file name
sink("my_data.txt")

#define data frame to write to file
df frame(player=c('A', 'B', 'C', 'D','E'),
                 points=c(12, 29, 24, 30, 19),
                 assists=c(5, 5, 7, 4, 10))

print(df)

#close the external connection
sink()

We can then navigate to the current working directory and open the text file:

The file contains the data frame that we created.

Example 3: Use sink() to Export Data Frame to CSV File

We can use the following sink() function to export a data frame to a CSV file:

#define file name
sink("my_data.csv")

#define data frame to write to file
df frame(player=c('A', 'B', 'C', 'D','E'),
                 points=c(12, 29, 24, 30, 19),
                 assists=c(5, 5, 7, 4, 10))

print(df)

#close the external connection
sink()

We can then navigate to the current working directory and open the CSV file:

The CSV file contains the data frame that we created.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Export a Data Frame to an Excel File in R
How to Export a Data Frame to a CSV File in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories