Read TXT Files in R with read.table()

In R language, the read.table() function can be used to read data from a txt file. Here is a simple example:

# 读取txt文件数据
data <- read.table("data.txt", header=TRUE)

# 查看数据框的结构
str(data)

# 查看数据框的前几行数据
head(data)

In this example, we assume that there is a txt file named “data.txt” containing data. The read.table() function reads the data in this file into a data frame and assigns the data frame to a variable named “data”. Setting header=TRUE indicates that the first row is the column names.

When reading data from a txt file, it is important to ensure that the file path is correct and that the data format in the file is correct, so that R language can read it correctly.

bannerAds