![]() |
Reading and writing rectangular text data quickly in R Programming Language can be achieved using various packages and functions, depending on your specific needs and the data format. Two commonly used packages for this purpose are readr and data. table. Here’s how you can do it with these packages. Rectangular Text Data
To Read Rectangular Text Data, it’s important to define the delimiter, data types, and whether the first row contains column names. Saving time and memory through efficient reading is possible. To Write Rectangular Text Data, export your R data structures, such as data frames, to text files. The output file’s location can be specified, along with the delimiter and character string quoting options. There are various packages for reading and writing rectangular text data. Let’s talk about the two major packages that support CSV file, txt file, etc. Different Packages To Read and Write Rectangular Text Datadata.table The data.table package generates a powerful and fast data manipulation framework in R. It is a reliable option when speed and performance are required because of its well-known efficiency in processing massive information. It is only for both CSV and txt files. Functions to Use
readr – The readr package is a component of the tidyverse ecosystem, a collection of R packages focused to simplify and speed up data manipulation and analysis. Readr also specializes in quickly and accurately reading rectangular text data sources. It is only for CSV files. Functions to use
Create Rectangular text dataLet’s create a Rectangular text data of columns named “Name” , “Age”, “City” and story value in data.txt. Create a dataframe with columns “Name”, “Age”, “City” and store its value in respective column. And save the data frame to a tab-delimited text file and create a text file with name “data.txt” Step 1: Create a dataframe with columns “Name”, “Age”, “City” and store its value in respective column. data.frame() function create a dataframe. It has three columns: “Name”, “Age”, and “City”. Values for each column are provided using the c() function, which concatenates values into a vector for each column. Step2: Save the data frame to a tab-delimited text file and create a text file with name “data.txt write.table() function save the dataframe to a text file. R
Output: Name Age City We can get this file in files section in R enviourment. ![]() How to Read and Write Rectangular Text Data Quickly This code will create a table in text document named as “data.txt” and store at the same folder where R studio is Efficient Data Manipulation with data.tableNow, it’s time to install package “data.table” and load it Activating the “data.table” Package (library(data.table)) – This line activates the “data.table” package, This package generates a powerful and fast data manipulation framework in R. R
Reading Rectangular Text Data (for txt file)Read data.txt with tab delimiter, headers, and specified column types as name is character, age is numeric and country is character.
R
Output: Name Age City Writing Rectangular Text Data (for txt file)Assuming ‘data’ is your rectangular data. Write data to output.txt with tab delimiter, no quoting! Data is the dataframe that will be written to the text file “Output.txt”. It is an output file. ‘sep = “\t”‘ sets the separator as a tab. ‘quote = FALSE’ state that quotes are not used around character fields. R
Output: Name Age City Reading and writing Rectangular Text Data in CSV fileLet’s say we have datset containing employee information about their salary and department. So, we want information of Gross having greater than 4000. The output file will contain the information of Gross having greater than 4000 only. Use dataset Employee_monthly_salary Step 1 – Activating the “data.table” Package (library(data.table)) – This line activates the “data.table” package, This package generates a powerful and fast data manipulation framework in R. Step 2 – Large CSV File Reading into a Data Table (fread()) fread(“Employee_monthly_salary.csv”) reads a large CSV file and loads it into a data table named large_data. The “data.table” package’s method fread() is used to read data, use the fread function and the file location. Step 3 – Rows Are Filtered Based on the Criteria ([GROSS > 50000]) ! The filtering function large_data[GROSS > 50000] allows only the rows of the large_data data table where the “Gross payment” (likely a column in the CSV) is greater than 50000. This uses the “data.table” syntax for a conditional subsetting action. Step 4- The filtered data is kept in a brand-new table called filtered_data. Filtered Data Writing to a New CSV File (fwrite()). The function fwrite(filtered_data, “filtered_sales.csv”) generates a new CSV file called “filtered_sales.csv” from the filtered data contained in filtered_data. R
Output: ![]() Output Reading and writing Rectangular Text Data Using readr PackageSuppose we have datset containing student information about their name, class and marks etc. So, we want information of category = ‘ female’. The output file will contain the information of female student only. The readr package is primarily used for reading structured text data into R. It provides functions to efficiently read various types of delimited files like CSV, TSV, and fixed-width format files. Here are some additional examples and explanations of how to use the readr package in R. Use dataset StudentsPerformance Step 1: The “readr” package from CRAN (Comprehensive R Archive Network) get installed with install.packages(“readr”). The R environment is loaded with the “readr” package within library(readr). Step 2: The read_csv() function only reads the CSV file into a dataframe: read_csv(), a function from the “readr” package, reads a CSV file and creates a dataframe. sales_data – read_csv(“StudentsPerformance.csv”) reads the CSV file “StudentsPerformance.csv” and load the data in a dataframe named sales_data. Step 3: modified_data <- sales_data’female’ is the value for sales_data$gender. Adjusting the sales_data dataframe to only contain rows where the “gender” column is “female” results in the creation of a new dataframe called modified_data. To filter rows based on the “gender” column, indexing and a logical condition are used. Step 4: Write_csv(modified_data, “femalecategory.csv”) generates a new CSV file called “femalecategory.csv” from the modified_data dataframe. The “readr” package’s write_csv() function writes a dataframe to a CSV file. R
Output: ![]() output Conclusionreading and writing rectangular text data efficiently in R can be achieved using two widely used packages: readr and data.table. These packages provide functions that are optimized for speed and memory efficiency, making them suitable for working with both small and large datasets. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |