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