R语言 读取文本文件的内容 – read.table() 函数
R语言中的 read.table() 函数是用来从一个文本文件中读取数据。它以表格的形式返回数据。
语法:
read.table(filename, header = FALSE, sep = “”)
参数:
header: 表示文件是否包含头行
sep: 表示文件中使用的分隔符值
例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