Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Reading Data from Files | Reading and Writing Files
Working with Text, Dates, and Files in R

bookReading Data from Files

Before you can analyze data in R, you must first read it into your workspace. Reading data from files is usually the first step of any data analysis project. Data often comes in a variety of file formats, with the most common being CSV (comma-separated values), Excel spreadsheets, and plain text (TXT) files. Each format has its own structure and requirements, but R provides dedicated functions to help you import these files efficiently.

123
# Import data from a CSV file into a data frame sales_data <- read.csv("sales_data.csv", header = TRUE) print(head(sales_data))
copy

In the code above, you see how to use the read.csv() function to import a CSV file. The header argument specifies whether the first row of the file contains column names. Most CSV files use the first row for headers, so header = TRUE is common. Typical CSV files separate values with commas and organize data in rows and columns, making them easy to load into R as data frames.

123
# Read a TXT file with custom separator and header products <- read.table("products.txt", sep = "\t", header = TRUE) print(head(products))
copy

The read.table() function is more general than read.csv() and can handle files with different delimiters. In the example above, the sep argument specifies the separator—in this case, a tab character ("\t"), which is common for TXT files. You can use read.table() for any text file by adjusting the sep and header arguments to match the file's format. Unlike read.csv(), which assumes commas as separators, read.table() lets you define custom delimiters, making it suitable for a wider range of file types.

Note
Definition

A data frame in R is a table-like structure for storing datasets, where each column can contain values of different types (numeric, character, etc.), and each row represents an observation.

When reading files into R, you might encounter problems such as encoding mismatches, missing values, or unexpected delimiters. If your file contains special characters, try specifying the fileEncoding argument. For missing values, use the na.strings argument to tell R which symbols represent missing data. Always check the structure of your data after importing to ensure it matches your expectations.

1. Which function is commonly used to read CSV files in R?

2. What argument would you use to specify the separator in read.table()?

3. Fill in the blank: To read a file with tab-separated values, use read.table('file.txt', sep='___').

question mark

Which function is commonly used to read CSV files in R?

Select the correct answer

question mark

What argument would you use to specify the separator in read.table()?

Select the correct answer

question-icon

Fill in the blank: To read a file with tab-separated values, use read.table('file.txt', sep='___').

read.table('file.txt', sep=)
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookReading Data from Files

Deslize para mostrar o menu

Before you can analyze data in R, you must first read it into your workspace. Reading data from files is usually the first step of any data analysis project. Data often comes in a variety of file formats, with the most common being CSV (comma-separated values), Excel spreadsheets, and plain text (TXT) files. Each format has its own structure and requirements, but R provides dedicated functions to help you import these files efficiently.

123
# Import data from a CSV file into a data frame sales_data <- read.csv("sales_data.csv", header = TRUE) print(head(sales_data))
copy

In the code above, you see how to use the read.csv() function to import a CSV file. The header argument specifies whether the first row of the file contains column names. Most CSV files use the first row for headers, so header = TRUE is common. Typical CSV files separate values with commas and organize data in rows and columns, making them easy to load into R as data frames.

123
# Read a TXT file with custom separator and header products <- read.table("products.txt", sep = "\t", header = TRUE) print(head(products))
copy

The read.table() function is more general than read.csv() and can handle files with different delimiters. In the example above, the sep argument specifies the separator—in this case, a tab character ("\t"), which is common for TXT files. You can use read.table() for any text file by adjusting the sep and header arguments to match the file's format. Unlike read.csv(), which assumes commas as separators, read.table() lets you define custom delimiters, making it suitable for a wider range of file types.

Note
Definition

A data frame in R is a table-like structure for storing datasets, where each column can contain values of different types (numeric, character, etc.), and each row represents an observation.

When reading files into R, you might encounter problems such as encoding mismatches, missing values, or unexpected delimiters. If your file contains special characters, try specifying the fileEncoding argument. For missing values, use the na.strings argument to tell R which symbols represent missing data. Always check the structure of your data after importing to ensure it matches your expectations.

1. Which function is commonly used to read CSV files in R?

2. What argument would you use to specify the separator in read.table()?

3. Fill in the blank: To read a file with tab-separated values, use read.table('file.txt', sep='___').

question mark

Which function is commonly used to read CSV files in R?

Select the correct answer

question mark

What argument would you use to specify the separator in read.table()?

Select the correct answer

question-icon

Fill in the blank: To read a file with tab-separated values, use read.table('file.txt', sep='___').

read.table('file.txt', sep=)
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
some-alt