如何在R中的cut in read.csv中指定文件名而不是完整的文件路径?

如何在R中的cut in read.csv中指定文件名而不是完整的文件路径?,r,R,它抛出一个错误,说cut:fl:没有这样的文件或目录。一般来说,R喜欢字符串作为许多输入: fl <- /home/somefile.csv; x <- read.csv(pipe("cut -f3 -d',' fl")); 可以使用“粘贴”将文件路径字符串与shell命令组合在一起 setwd("/home/user/folder") #sets your working directyory list.files() #lists the files in the work

它抛出一个错误,说cut:fl:没有这样的文件或目录。

一般来说,R喜欢字符串作为许多输入:

fl <- /home/somefile.csv;
x <- read.csv(pipe("cut -f3 -d',' fl"));
可以使用“粘贴”将文件路径字符串与shell命令组合在一起

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

你能像上面那样为x重写吗?你可以试着用R写命令。@Serban:非常感谢你给我一点具体的直接建议。x非常感谢,它能工作:这没关系,但问题是我的文件中有不止一列,我只需要读第三列,因此我使用了pipe and cut,但是文件名不是静态的,因此尝试传递文件名而不是完整路径,但不起作用。
# 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