![]() |
VOOZH | about |
In today's data-driven world, collecting data from multiple sources and turning it into a structured manner is a critical responsibility for data analysts and scientists. Text files are a prominent source of data, as they frequently include useful information in plain text format. To be used successfully, this data must be translated into a structured format, such as a DataFrame, which is a two-dimensional, size-mutable, heterogeneous tabular data structure with labeled axes.
Reading text files in R Programming Language is the process of taking data from plain text files and transforming it into a structured format that is easy to edit and analyze. Here are the types of text files available.
There are three main methods :
Let's take an example that you have a data frame df with student information loaded into a csv file.
The data contains three columns: "Name", "Roll No", and "Marks".
CSV files are commonly used to store tabular data. Here's how to read CSV files into a DataFrame using R:
For import your dataset you can take any dataset and replace the path in code.
Output:
age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal target
1 52 1 0 125 212 0 1 168 0 1.0 2 2 3 0
2 53 1 0 140 203 1 0 155 1 3.1 0 0 3 0
3 70 1 0 145 174 0 1 125 1 2.6 0 0 3 0
4 61 1 0 148 203 0 1 161 0 0.0 2 1 3 0
5 62 0 0 138 294 1 1 106 0 1.9 1 3 2 0
6 58 0 0 100 248 0 0 122 0 1.0 1 0 2 1The read.delim() method reads data from the file "data.tsv". Values in TSV files are separated by tabs, and this function defaults to using the tab (\t) delimiter.
Output:
age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal target
1 52 1 0 125 212 0 1 168 0 1.0 2 2 3 0
2 53 1 0 140 203 1 0 155 1 3.1 0 0 3 0
3 70 1 0 145 174 0 1 125 1 2.6 0 0 3 0
4 61 1 0 148 203 0 1 161 0 0.0 2 1 3 0
5 62 0 0 138 294 1 1 106 0 1.9 1 3 2 0
6 58 0 0 100 248 0 0 122 0 1.0 1 0 2 1
Tabular files store data in rows and columns. How to read tabular files into a DataFrame in R:
Output:
age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal target
1 52 1 0 125 212 0 1 168 0 1.0 2 2 3 0
2 53 1 0 140 203 1 0 155 1 3.1 0 0 3 0
3 70 1 0 145 174 0 1 125 1 2.6 0 0 3 0
4 61 1 0 148 203 0 1 161 0 0.0 2 1 3 0
5 62 0 0 138 294 1 1 106 0 1.9 1 3 2 0
6 58 0 0 100 248 0 0 122 0 1.0 1 0 2 1
Reading text files into a DataFrame in R is an important step in the data analysis process. Analysts can efficiently extract, modify, and analyse data from a variety of sources using R functions and packages. Understanding various text file reading methods and proper data management procedures guarantees that R analysis findings are reliable and meaningful.