[英]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.