How can R language convert a txt file into a data frame?

In R language, you can use the read.table() function to convert a txt file into a data frame. The specific steps are as follows:

  1. Change the working directory.
  2. “use the read.table() function”

The example code is as follows:

# 设置工作目录
setwd("path/to/working/directory")

# 读取txt文件并转化为数据框
data <- read.table("filename.txt", header = TRUE, sep = "\t")

path/to/working/directory is the path to the working directory, and filename.txt is the name of the txt file to be converted. Setting header = TRUE indicates that the txt file contains column names, and sep = “\t” specifies that the data is separated by tabs.

Once reading is complete, the data will be stored in a dataframe named data.

bannerAds