6.9 C
London
Friday, December 27, 2024
HomeRImport & Export Data in RHow to Import TSV Files into R (Including Example)

How to Import TSV Files into R (Including Example)

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 following basic syntax to import a TSV file into R:

library(readr)

#import TSV file into data frame
df C:/Users/bob/Downloads/data.tsv')

The following examples show how to use this syntax in practice.

Example 1: Import TSV File into R (With Column Names)

Suppose I have the following TSV file called data.tsv saved somewhere on my computer:

I can use the following syntax to import this TSV file into a data frame in R:

library(readr)

#import TSV file into data frame
df C:/Users/bob/Downloads/data.tsv')

#view data frame
df

# A tibble: 5 x 3
  team  points rebounds
      
1 A         33       12
2 B         25        6
3 C         31        6
4 D         22       11
5 E         20        7

We can see that the TSV file was successfully imported into R.

Example 2: Import TSV File into R (No Column Names)

Suppose I have the following TSV file called data.tsv with no column names:

I can use the col_names argument to specify that there are no column names when importing this TSV file into R:

library(readr)

#import TSV file into data frame
df C:/Users/bob/Downloads/data.tsv', col_names=FALSE)

#view data frame
df

  X1       X2    X3
    
1 A        33    12
2 B        25     6
3 C        31     6
4 D        22    11
5 E        20     7

By default, R provides the column names X1, X2, and X3.

I can use the following syntax to easily rename the columns:

#rename columns
names(df) team', 'points', 'rebounds')

#view updated data frame
df

  team  points rebounds
        
1 A         33       12
2 B         25        6
3 C         31        6
4 D         22       11
5 E         20        7

Note: You can find the complete documentation for the read_tsv() function here.

Additional Resources

The following tutorials explain how to import other files in R:

How to Import CSV Files into R
How to Import a CSV from URL in R
How to Import Excel Files into 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