简体   繁体   English

R - 在 for 循环中打印 object 的名称

[英]R - print name of object in a for loop

I'm applying some code to a number of files using a for loop in R.我正在使用 R 中的 for 循环将一些代码应用于许多文件。 I want to save an output file after each iteration of the loop, named according to the file being processed in that iteration of the loop.我想在每次循环迭代后保存一个 output 文件,根据循环迭代中正在处理的文件命名。 How do I print the name of the file being used in each round of the loop?如何打印每轮循环中使用的文件的名称?

My understanding of a for loop is that you supply it with a list and some code , it then runs through each item on the list and applies the code to it.我对 for 循环的理解是,您向它提供一个list和一些code ,然后它会遍历list的每个项目并将code应用于它。 So it seems to me that I should be able to pull out the name of the item on the list being used in each round of the loop.所以在我看来,我应该能够在循环的每一轮中提取list正在使用的项目的名称。 Please correct me if this is wrong.如果这是错误的,请纠正我。

After a lot of searching, this is the closest I've been able to get:经过大量搜索,这是我能够得到的最接近的:

# load lines files
csv1 <- read.csv("csv1.csv")
csv2 <- read.csv("csv2.csv")

# make list of lines files
object.list <- list(csv1, csv2)

for(i in object.list){
  print(deparse(substitute(i)))
}

Which produces:产生:

[1] "i"
[1] "i"

But I want it to produce:但我希望它产生:

[1] "csv1"
[1] "csv2"

Any ideas?有任何想法吗? Simple examples please.请举个简单的例子。

What about naming the elements of the list:如何命名列表的元素:

# load lines files
csv1 <- "file content"
csv2 <- "some other file content"

# make list of lines files
object.list <- list(csv1 = csv1, csv2 = csv2)

for(name in names(object.list)){
  print(name)
  print(object.list[[name]])
}

I do not believe there is a direct way of doing it.我不相信有直接的方法可以做到这一点。 R does not remember that i has been created using csv1 and csv2 . R 不记得i是使用csv1csv2创建的。 It only knows the values.它只知道值。 Maybe this workaround works for you:也许这种解决方法对您有用:

object.list <- list(csv1, csv2)
names(object.list) <- c( substitute(csv1), substitute(csv2))

for(i in 1:length(object.list){
  print(names(object.list[i]))
}

Adding the original variables as names you can access them later.添加原始变量作为名称,您可以稍后访问它们。

You may be better served by changing your work flow... Here's an example using some csv files I have laying around更改工作流程可能会为您提供更好的服务...这是使用我放置的一些 csv 文件的示例

# You could also do this via a system call
# No need for a list a vector will do
files_to_read <- c("bfwicox.csv", "churn.csv")
# Name automatically
names(files_to_read) <- files_to_read

# Now run loop
for(name in names(files_to_read)){
  data <- read.csv(files_to_read[[name]])
  print(paste("This is a summary of the first two variables in file...", 
              files_to_read[[name]]))
  print(summary(data[1:2]))
}

[1] "This is a summary of the first two variables in file... bfwicox.csv"
       X             income      
 Min.   :    1   Min.   :  1100  
 1st Qu.: 4250   1st Qu.: 25000  
 Median : 8500   Median : 40000  
 Mean   : 8500   Mean   : 50687  
 3rd Qu.:12750   3rd Qu.: 65000  
 Max.   :16999   Max.   :250000  
[1] "This is a summary of the first two variables in file... churn.csv"
      ID               ACLENGTH    
 Length:5000        Min.   :  0.0  
 Class :character   1st Qu.: 73.0  
 Mode  :character   Median :100.0  
                    Mean   :100.9  
                    3rd Qu.:127.0  
                    Max.   :227.0  

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

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