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”

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.
file = "data.txt" confirm the file name to be "data.txt".
sep = "\t" sets the separator as a tab.
row.names = FALSE says that row names are not present in the output.
quote = FALSE state that quotes are not used around character fields.

R




# Create a dataframe with columns "Name", "Age", "City" and store its value in respective column
data <- data.frame(
  Name = c("Mina", "Nora", "Tina", "Ram"),
  Age = c(25, 30, 22, 28),
  City = c("India", "Germany", "NY", "America")
)
# Save the data frame to a tab-delimited text file and create a text file
#with name "data.txt"
write.table(data, file = "data.txt", sep = "\t", row.names = FALSE,
            quote = FALSE)


Output:

Name    Age    City
Mina 25 India
Nora 30 Germany
Tina 22 NY
Ram 28 America

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.table

Now, 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




install.packages("data.table")
 
library(data.table)


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.

  • fread() is a function from the “data.table” package in R reads for fast and efficient data from different file formats.
  • header = TRUE confirms that the first row in the file contains column names.
  • colClasses = c(“character”, “numeric”, “character”) tells the classes of the columns in the output dataframe.
  • “data.txt” is the file path of the text file to be read.

R




# Read data.txt with tab delimiter, headers, and specified column types
#as name is character, age is numeric and country is character
 
data <- fread("data.txt", sep = "\t", header = TRUE,
              colClasses = c("character", "numeric", "character"))
 
data


Output:

   Name Age    City
1: Mina 25 India
2: Nora 30 Germany
3: Tina 22 NY
4: Ram 28 America

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




# Write data to output.txt with tab delimiter, no quoting
 
fwrite(data, "output.txt", sep = "\t", quote = FALSE)


Output:

   Name Age    City
1: Mina 25 India
2: Nora 30 Germany
3: Tina 22 NY
4: Ram 28 America

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