R 编程中的文件处理
在 R 编程中,可以使用 R 基础包中的内置函数来处理文件,例如读取和写入文件。在本文中,让我们讨论 CSV 文件的读写、创建文件、重命名文件、检查文件是否存在、列出工作目录中的所有文件、复制文件和创建目录。
创建文件
使用file.create()
函数,可以从控制台创建一个新文件,如果已经存在则截断。如果文件被创建,该函数返回 TRUE 逻辑值,否则返回 FALSE。
Syntax:
file.create(” “)
Parameters:
” “: name of the file is passed in ” ” that has to be created
例子:
# Create a file
# The file created can be seen
# in your working directory
file.create("GFG.txt")
输出:
[1] TRUE
写入文件
R 编程中的write.table()
函数用于将对象写入文件。此函数存在于 R 的utils包中,并将数据框或矩阵对象写入任何类型的文件。
Syntax:
write.table(x, file)
Parameters:
x: indicates the object that has to be written into the file
file: indicates the name of the file that has to be written
例子:
# Write iris dataset
# into the txt file
write.table(x = iris[1:10, ],
file = "GFG.txt")
输出:
重命名文件
file.rename()
函数重命名文件并返回一个逻辑值。该函数重命名文件但不重命名目录。
Syntax:
file.rename(from, to)
Parameters:
from: indicates current file name or path
to: indicates new file name or path
例子:
# Rename file GFG.txt to newGFG.txt
file.rename("GFG.txt", "newGFG.txt")
输出:
[1] TRUE
检查文件是否存在
可以使用file.exists()
函数检查当前工作目录中是否存在文件。如果文件存在则返回 TRUE 逻辑值,否则返回 FALSE。
Syntax:
file.exists(” “)
Parameters:
” “: name of the file that has to be searched in the current working directory is passed in ” ”
例子:
# Check for GFG>txt
file.exists("GFG.txt")
# Check for newGFG.txt
file.exists("newGFG.txt")
输出:
[1] FALSE
[1] TRUE
读取文件
使用 R 中的read.table()
函数,可以读取文件并将输出显示为数据框。此功能有助于分析数据框以进行进一步计算。
Syntax:
read.table(file)
Parameters:
file: indicates the name of the file that has to be read
例子:
# Reading txt file
new.iris <- read.table(file = "GFG.txt")
# Print
print(new.iris)
X Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 1 5.1 3.5 1.4 0.2 setosa
2 2 4.9 3.0 1.4 0.2 setosa
3 3 4.7 3.2 1.3 0.2 setosa
4 4 4.6 3.1 1.5 0.2 setosa
5 5 5.0 3.6 1.4 0.2 setosa
6 6 5.4 3.9 1.7 0.4 setosa
7 7 4.6 3.4 1.4 0.3 setosa
8 8 5.0 3.4 1.5 0.2 setosa
9 9 4.4 2.9 1.4 0.2 setosa
10 10 4.9 3.1 1.5 0.1 setosa
列出所有文件
使用list.files()
函数,指定路径的所有文件都将显示在输出中。如果函数参数中未传递路径,则当前工作目录中存在的文件将显示为输出。
Syntax:
list.files(path)
Parameters:
path: indicates the path
To know about more optional parameters, use below command in console: help(“list.files”)
例子:
# Show all files in
# current working directory
list.files()
输出:
[1] "Bandicam" "Bluetooth Folder"
[3] "Book1.xlsx" "Custom Office Templates"
[5] "desktop.ini" "list.xlsx"
[7] "My Music" "My Pictures"
[9] "My Videos" "newGFG.txt"
[11] "Visual Studio 2012"
复制文件
R 中的file.copy()
函数有助于从控制台本身创建指定文件的副本。
Syntax:
file.copy(from, to)
Parameters:
from: indicates the file path that has to be copied
to: indicates the path where it has to be copied
To know about more optional parameters, use below command in console: help(“file.copy”)
例子:
# Copying
file.copy("C:/Users/Utkarsh/Documents/
newGFG.txt", "E:/")
# List the files in E:/ drive
list.files("E:/")
输出:
[1] TRUE
[1] "$RECYCLE.BIN" "Counter-Strike Global Offensive"
[3] "Home" "newGFG.txt"
[5] "System Volume Information"
创建目录
dir.create()
函数在函数参数中指定的路径中创建一个目录。如果函数参数中未指定路径,则在当前工作目录中创建目录。
Syntax:
dir.create(path)
Parameters:
path: indicates the path where directory has to be created with directory name at the end of the path
例子:
# Without specifying the path,
# directory will be created in
# current working directory
dir.create("GFG")
# List files
list.files()
输出:
[1] "Bandicam" "Bluetooth Folder"
[3] "Book1.xlsx" "Custom Office Templates"
[5] "desktop.ini" "GFG"
[7] "list.xlsx" "My Music"
[9] "My Pictures" "My Videos"
[11] "newGFG.txt" "Visual Studio 2012"