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:

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.

Similar Reads

Rectangular Text Data

In R, to manipulate and analyze data, reading and writing rectangular text data is an important activity. Rows and columns of rectangular text data get divided in a tabular layout and are frequently separated by commas or tabs. Rectangular text data is ordered as rows and columns of structured data, commonly stored in text files. Every observation is represented by a row, and every variable or field is represented by a column....

Create Rectangular text data

Let’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”...

Reading and writing Rectangular Text Data in CSV file

...

Reading and writing Rectangular Text Data Using readr Package

...

Conclusion

...

Contact Us