在 R 编程中读取文本文件的内容 – read.table()函数
R 语言中的read.table()
函数用于从文本文件中读取数据。它以表格的形式返回数据。
Syntax:
read.table(filename, header = FALSE, sep = “”)
Parameters:
header: represents if the file contains header row or not
sep: represents the delimiter value used in file
示例 1:从同一目录读取数据
# R program to read a text file
# Get content into a data frame
data <- read.table("TextFileExample.txt",
header = FALSE, sep = " ")
# Printing content of Text File
print(data)
输出:
V1 V2 V3
1 100 A a
2 200 B b
3 300 C c
4 400 D d
5 500 E e
6 600 F f
示例 2:从不同目录读取数据
# R program to read a text file
# Reading data from another directory
x<-read.table("D://Data//myfile.txt", header = FALSE)
# print x
print(x)
输出:
V1 V2 V3
1 100 a1 b1
2 200 a2 b2
3 300 a3 b3