Reading and writing Rectangular Text Data in CSV file
Let’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
library (data.table) # Read a large CSV file into a data table large_data <- fread ( "Employee_monthly_salary.csv" ) # Filter rows where the 'Gross payment' column is greater than 50000 filtered_data <- large_data[ 'GROSS' > 50000] # Write the filtered data to a new CSV file fwrite (filtered_data, "filtered_sales.csv" ) |
Output:
Read and Write Rectangular Text Data Quickly using R
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.