简体   繁体   English

在 R 中加载 csv 文件

[英]Load csv file in R

is there a way to load a csv file in R and define the variable automatically from the filename?有没有办法在 R 中加载 csv 文件并从文件名自动定义变量? So, if you have a csv file called 'hello', can I load it in R and create the df/var.所以,如果你有一个名为“hello”的 csv 文件,我可以在 R 中加载它并创建 df/var. without defining it?没有定义它?

So, rather than define hello in the load procedure: hello=read("filepath/hello");因此,不要在加载过程中定义 hello: hello=read("filepath/hello"); instead we have read("filepath/hello") but include a command to create and name a variable that is the same name of the file name (hello in this example?)相反,我们已经 read("filepath/hello") 但包含一个命令来创建和命名一个与文件名相同的变量(在这个例子中是 hello?)

I have advised you not to do this in any real world scenario, but if it helps understanding the concepts, this is not a complete solution but the important ingredients.我建议您不要在任何现实世界的场景中这样做,但如果它有助于理解概念,这不是一个完整的解决方案,而是重要的成分。

<<- the superassignement operator in the enclosing environment, which in the following case is the global namespace: <<-封闭环境中的超赋值运算符,在以下情况下是全局命名空间:

rm(hello) # just in case, ignore warning if there is any

dont <- function(){
    hello <<- 42
}

print(hello)
dont()
print(hello)

So you can define values in the enclosing environment within a function without a return value.因此,您可以在没有返回值的 function 中定义封闭环境中的值。

The name of that variable does not have to be fixed (as hello in the example above) but can depend on an argument to that function as in该变量的名称不必固定(如上面示例中的hello ),但可以取决于该 function 的参数,如

dontdothis <-  function(name){
   eval(parse(text = paste0(name, " <<- 42")))
}

dontdothis("frederik")
print(frederik * 2)

You will need to add the file operations and some small detail but that is how one could do it.您将需要添加文件操作和一些小细节,但这是可以做到的。 You may want to google for namespaces and environments and assignment operators in R to get a better understanding of the details in there.您可能想在 R 中搜索命名空间和环境以及赋值运算符,以便更好地了解其中的细节。

Worthwhile short read to distinguish between global environment and enclosing environment: Why is using `<<-` frowned upon and how can I avoid it?值得一读以区分全局环境和封闭环境: 为什么使用 `<<-` 不受欢迎,我该如何避免?

Depending on why you would like to do this I would offer you another solution:根据您为什么要这样做,我会为您提供另一种解决方案:

I suppose your problem is that you have a big folder with a lot of csv files and you would like to load them all and give the variables the name of the csv file without typing everything manually.我想你的问题是你有一个大文件夹,里面有很多 csv 文件,你想全部加载它们并给变量 csv 文件的名称,而不需要手动输入所有内容。

Then you can run然后你可以运行

> setwd("C:/Users/Testuser/testfiles")
> file_names <- list.files()
> file_names
[1] "rest"      "test1.txt" "test2.csv" "test3.csv"

where as path you use the path where all your csv files are stored.作为路径,您使用存储所有 csv 文件的路径。

Then if there are stored any other files and you only would like to have the csv files we have to grep them with regex然后,如果存储了任何其他文件并且您只想拥有 csv 文件,我们必须使用正则表达式对它们进行 grep

> file_names_csv <- file_names[grepl(".csv",file_names)]
> file_names_csv
[1] "test2.csv" "test3.csv"

Now we load them with a for loop and assign them to a variable that is named as the corresponding csv file现在我们使用 for 循环加载它们并将它们分配给名为相应 csv 文件的变量

for( name in file_names_csv){
  assign(paste(name, sep=""), read.csv(file = paste(name, sep="")))
}

And we have我们有

> test2.csv
  test
1 1234
> test3.csv
  test
1 2323

You can also gsub the.csv away before you load the data by您也可以在加载数据之前 gsub the.csv

> file_names_csv <- gsub(".csv","",file_names_csv )
> file_names_csv
[1] "test2" "test3"

So basically you have exactly what you have asked for without using global variables.所以基本上你有你所要求的,而不使用全局变量。

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

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