简体   繁体   English

for(seq_along(data_file)中的i:file(file,“ rt”)中的错误:无效的'description'参数

[英]for(i in seq_along(data_file): Error in file(file, “rt”) : invalid 'description' argument

I begin by reading data into R via an Excel file which contains a list of file location and file names: 我首先通过一个Excel文件将数据读入R,该文件包含文件位置和文件名的列表:

data_files <- readxl::read_excel("./Data/source_data.xlsx")

Here is what it looks like: 看起来像这样: 在此处输入图片说明

Next I want to iterate over this list using a for loop/seq_along to access each row variable and use the row variable within the read.csv() and then do things: 接下来,我想使用for循环/ seq_along遍历此列表,以访问每个行变量,并在read.csv()中使用行变量,然后执行以下操作:

for(i in seq_along(data_files)){
  x <- read.csv(data_files[[i]], sep= "\t", skipNul = TRUE)
  # DO THINGS
}

When I run the for loop, I get: 当我运行for循环时,我得到:

Error in file(file, "rt") : invalid 'description' argument

I know it has to do with the iteration not occurring in a manner that I expect because when I do the following the csv is read into R successfully: 我知道这与未按我期望的方式发生迭代有关,因为在执行以下操作时,csv已成功读入R:

path <- "/Users/gerb/Downloads/"
file <- "Who_Has_Seen_crosstab (2).csv"
path_file <- paste(path, file, sep="")

x <- read.csv(path_file, sep= "\t", skipNul = TRUE)

@Rui Barradas gets my vote but it is worth expanding on his answers. @Rui Barradas得到我的投票,但值得扩大他的答案。 from ?seq_along : "seq_along and seq_len return an integer vector." from ?seq_along :“ seq_along和seq_len返回整数向量。” As mentioned you can see this by adding print into a function. 如前所述,您可以通过将print添加到函数中来查看此内容。

l = tibble(a = c("a","b","C"))
x = tibble(a = c("a","b","C"),
            b = c("x","y","z"))

for(i in seq_along(l)){
    print(l[i])
}

# A tibble: 3 x 1
   a    
 <chr>
1 a    
2 b    
3 C 


for(i in seq_along(x)){
    print(x[i])
 }

> for(i in seq_along(x)){
      print(x[i])
   }

 #A tibble: 3 x 1
 a    
<chr>
1 a    
2 b    
3 C    
# A tibble: 3 x 1
  b    
 <chr>
1 x    
2 y    
3 z   

You don't want the entire vector. 您不想要整个向量。 It seems that you want to iterate over each row in a tibble. 看来您想遍历小标题中的每一行。 There are others ways to do this, but something like this would be a better approach: 还有其他方法可以执行此操作,但是类似这样的方法会更好:

for(i in 1:nrow(l)){
    print(l[i,])
  }

# A tibble: 1 x 1
 a    
<chr>
1 a    
# A tibble: 1 x 1
  a    
 <chr>
1 b 
 .....   

Last point: it is easier to read a copy and paste of R output than a image. 最后一点:阅读R输出的副本和粘贴比图像更容易。 Avoiding screen grabs for future questions would be helpful for others. 避免抓住屏幕来回答将来的问题对其他人会有所帮助。

暂无
暂无

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

相关问题 R闪亮:文件错误(文件,“ rt”):无效的“描述”参数 - R shiny: Error in file(file, “rt”) : invalid 'description' argument 文件(文件,“rt”)中的错误:调用函数时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument , when calling the function 使用 seq_along 重命名文件时使用长度 &gt; 1 的文件名? - using length > 1 of a file name when rename files with seq_along? 文件错误(文件,“rt”):complete.cases 程序中的“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument in complete.cases program 文件错误(文件,“rt”):读取 csv 文件时“描述”参数无效 - Error in file(file, "rt") : invalid 'description' argument when reading csv files 文件中的错误(文件,ifelse(追加,“a”,“w”)):无效的“描述”参数 - Error in file(file, ifelse(append, “a”, “w”)) : invalid 'description' argument 如何解决错误“文件错误:无效的&#39;description&#39;参数? - How to resolve error " Error in file : invalid 'description' argument? 文件错误(文件“ rt”) - Error in file(file, “rt”) 使用 openxlsx 将 Excel 数据导入 R:文件中的错误(con,“r”):无效的“描述”参数 - Import Excel data into R using openxlsx: Error in file(con, "r") : invalid 'description' argument 类似于分组数据上的条件seq_along - Something like conditional seq_along on grouped data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM