我如何给文件名,而不是完整的文件路径切割在read.csv中的R


fl <- /home/somefile.csv;
x <- read.csv(pipe("cut -f3 -d',' fl"));

它抛出一个错误,说cut: fl: No such file or directory .

您可以使用paste将文件路径字符串与shell命令

结合使用。
# Create some example data
write.csv(mtcars, "somefile.csv")
# Define filepath as a character string
fl <- "somefile.csv"
# Read in third column by pasteing strings together
x <- read.csv(pipe(paste("cut -f3 -d',' ", fl)))
head(x)
#  cyl
#1   6
#2   6
#3   4
#4   6
#5   8
#6   6

一般来说,R喜欢对许多输入使用字符串:

setwd("/home/user/folder")  #sets your working directyory
list.files()  #lists the files in the working directory
myfilename <- "somefile.csv"  #sets the file name
x <- read.csv(myfilename)  # reads file into x

最新更新