简体   繁体   English

如何在R中创建新的read.csv,使其无需输入.csv文件的全名就可以读取.csv文件

[英]How to create a new read.csv in R so it can read .csv file without typing the full name of .csv file

guys, thanks for read this. 伙计们,感谢您阅读本文。 This is my first time writing a program so pardon me if I make stupid questions. 这是我第一次编写程序,如果我提出愚蠢的问题,请原谅。

I have bunch of .csv files named like: 001-XXX.csv;002-XXX.csv...150-XXX.csv. 我有一堆名为.001的.csv文件:001-XXX.csv; 002-XXX.csv ... 150-XXX.csv。 Here XXX is a very long name tag. XXX是一个很长的名称标签。 So it's a little annoying that every time I need to type read.csv("001-xxx.csv"). 因此,每次我需要键入read.csv(“ 001-xxx.csv”)时,都会感到有点烦。 I want to make a function called "newread" that only ask me for the first three digits, the real id number, to read the .csv files. 我要创建一个名为“ newread”的函数,该函数仅要求我输入前三位数字(真实ID号)即可读取.csv文件。 I thought "newread" should be like this: 我以为“ newread”应该是这样的:

newread <- function(id){
  as.character(id)
  a <- paste(id,"-XXX.csv",sep="")  
  read.csv(a)
}

BUt R shows Error: unexpected '}' in "}" What's going wrong? BUt R显示错误:“}”中出现意外的'}'怎么了? It looks logical. 看起来很合逻辑。

I am running Rstudio on Windows 8. 我在Windows 8上运行Rstudio。

as.character(id) will not change id into a character string. as.character(id)不会将id更改为字符串。 Change it to: 更改为:

id = as.character(id)

Edit: According to comments, you should call newread() with a character paramter, and there is no difference between newread(001) and newread(1) . 编辑:根据评论,您应该使用字符参数调用newread() ,并且newread(001)newread(1)之间没有区别。

This is not specifically an answer to your question (others have covered that), but rather some advice that may be helpful for accomplishing your task in a different way. 这并不是专门回答您的问题(其他人已经解决了这个问题),而是一些建议,可能以不同的方式帮助您完成任务。

First, some of the GUI's for R have file name completion. 首先,R的某些GUI具有文件名补全。 You can type the first part: read.csv("001- and then hit a key or combination of keys (In the windows GUI you press TAB) and the rest of the filename will be filled in for you (as long as it is unique). 您可以输入第一部分: read.csv("001- ,然后按一个键或多个键的组合(在Windows GUI中,按TAB键),其余文件名将为您填充(只要是独特)。

You can use the file.choose or choose.files functions to open a dialog box to choose your file using the mouse: read.csv(file.choose()) . 您可以使用file.choosechoose.files函数打开一个对话框,以使用鼠标选择文件: read.csv(file.choose())

If you want to read in all the above files then you can do this in one step using lapply and either sprintf or list.files (or others): 如果要读取上述所有文件,则可以使用lapplysprintflist.files (或其他文件)一步完成此操作:

mycsvlist <- lapply( 1:150, function(x) read.csv( sprintf("%03d-XXX.csv", x) ) )

or 要么

mvcsvlist <- lapply( list.files(pattern="\\.csv$"), read.csv )

You could also use list.files to get a list of all the files matching a pattern and then pass one of the returned values to read.csv : 您还可以使用list.files获取与模式匹配的所有文件的列表,然后将返回值之一传递给read.csv

tmp <- list.files(pattern="001.*csv$")
read.csv(tmp[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM