在 R 编程中读取 CSV 文件的内容 – read.csv()函数
R 语言中的read.csv()
函数用于读取“逗号分隔值”文件。它以数据框的形式导入数据。
Syntax:
read.csv(file, header, sep, dec)
Parameters:
file: the path to the file containing the data to be imported into R.
header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the name of each column. If that’s not the case, you can add the argument header = FALSE.
sep: the field separator character
dec: the character used in the file for decimal points.
示例 1:从同一文件夹中读取文件
# R program to read a csv file
# Get content into a data frame
data <- read.csv("CSVFileExample.csv",
header = FALSE, sep = "\t")
# Printing content of Text File
print(data)
输出:
V1 V2 V3
1 100 AB ab
2 200 CD cd
3 300 EF ef
4 400 GH gh
5 500 IJ ij
示例 2:从不同目录读取文件
# Simple R program to read csv file
x <- read.csv("D://Datas//myfile.csv")
# print x
print(x)
输出:
X V1 V2 V3
1 1 100 a1 b1
2 2 200 a2 b2
3 3 300 a3 b3