Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在for循环中使用read.table?_R - Fatal编程技术网

如何在for循环中使用read.table?

如何在for循环中使用read.table?,r,R,我想在for循环中使用read.table,一次对每个数据进行一些分析,并将结果写入矩阵。文件名具有以下模式AA.BB.CC.P1.DD,AA.BB.CC.P2.DD,等等 我试过这个: for(i in 1:8) { assign( paste("AA.BB.CC.P", i, sep="") data <- read.table(paste(i, "DD", sep=".")) a <- data$V7 b <- data$V12 c <- data$

我想在
for
循环中使用
read.table
,一次对每个数据进行一些分析,并将结果写入矩阵。文件名具有以下模式
AA.BB.CC.P1.DD
AA.BB.CC.P2.DD
,等等

我试过这个:

for(i in 1:8) {
  assign( paste("AA.BB.CC.P", i, sep="") 
    data <- read.table(paste(i, "DD", sep="."))

a <- data$V7
b <- data$V12
c <- data$V13
d <- data$V15

function(a,b,c,d)
  }
for(1:8中的i){
分配(粘贴(“AA.BB.CC.P”,i,sep=”“)
数据这对你有用吗?(我不确定你在用
函数(a,b,c,d)
做什么)

for(1:8中的i){
数据这对你有用吗?(我不确定你在用
函数(a,b,c,d)
做什么)

for(1:8中的i){
数据<代码>用于(1:8中的i){
数据<代码>用于(1:8中的i){

数据您的代码乱七八糟。不要在此处使用
for
循环。最简单的方法是将所有文件放在一个目录中,然后执行以下操作:

ff <- list.files(path="<add your dir path here>", full.names=TRUE)
#use the pattern argument, if you have files in the directory, 
#   which you don't want to read in

myfilelist <- lapply(ff, read.table)
names(myfilelist) <- list.files(path="<add your dir path here>", full.names=FALSE)

ff您的代码乱七八糟。不要在此处使用
for
循环。最简单的方法是将所有文件放在一个目录中,然后执行以下操作:

ff <- list.files(path="<add your dir path here>", full.names=TRUE)
#use the pattern argument, if you have files in the directory, 
#   which you don't want to read in

myfilelist <- lapply(ff, read.table)
names(myfilelist) <- list.files(path="<add your dir path here>", full.names=FALSE)

ff这个答案建立在罗兰的答案之上

要迭代文件中的项并将它们读入表中,可以使用罗兰的代码

问题是不能将非默认参数传递到read.table函数中

下面的代码创建了一个设置header=False和sep=','的函数,但您可以更改它并对自己的函数进行个性化设置(这解决了user2162153的问题)

#根据罗兰的答案,你可以传递一个个性化的阅读器函数。首先定义你的函数,然后使用他的代码。

这个答案建立在罗兰的答案之上

要迭代文件中的项并将它们读入表中,可以使用罗兰的代码

问题是不能将非默认参数传递到read.table函数中

下面的代码创建了一个设置header=False和sep=','的函数,但您可以更改它并对自己的函数进行个性化设置(这解决了user2162153的问题)

#根据罗兰的答案,你可以传递一个个性化的阅读器函数。首先定义你的函数,然后使用他的代码。


个性化\u阅读器您的代码正在尝试读取一个不存在的文件
data@Jaap。不,考虑到回复,现在它得到了文件名,但不是
data$V7
,等等。您的代码正在尝试读取一个不存在的文件
data@Jaap。不,考虑到回复,现在它得到了文件名,但不是
data$V7
,等等它似乎不是为了函数的目的一次读取每个文件。它说,
数据中的错误$V7:$运算符对原子向量无效
谢谢。它似乎不是为了函数的目的一次读取每个文件。它说,
数据中的错误$V7:$运算符对原子向量无效
谢谢!现在,它得到了文件名正确,但不接受
数据$V7
等。必须检查数据的标题,使用colnames(data)并使用该字段。单个文件没有标题。请尝试使用数据[,x]其中x是您要查找的coulmn的编号,例如:a谢谢!现在,它正确获取文件名,但不接受
数据$V7
,等等。您必须检查数据的标题,使用colnames(data)并使用该字段单个文件没有标题。请尝试使用数据[,x]其中x是您要查找的coulmn的编号,例如:a如何定义每个文件的标题?我尝试将其传递到
read.table
,但它不起作用。如何定义每个文件的标题?我尝试将其传递到
read.table
,但它不起作用。您能解释一下您的答案吗?代码块始终是mu你能解释一下你的答案吗?代码块总是信息量大得多,并附有解释。
data <- read.table(paste(i, "DD", sep="."))
ff <- list.files(path="<add your dir path here>", full.names=TRUE)
#use the pattern argument, if you have files in the directory, 
#   which you don't want to read in

myfilelist <- lapply(ff, read.table)
names(myfilelist) <- list.files(path="<add your dir path here>", full.names=FALSE)
#Building up on Roland's answer, you can pass a personalized reader function. First define your function and than use his codes. 

Personalized_Reader <- function(lambda){
 read.table(lambda, header = FALSE ,sep = ',')}


# Personalize your arguments on the read.table function

ff <- list.files(path="<add your dir path here>", full.names=TRUE)
#use the pattern argument, if you have files in the directory, which you don't want to read in

myfilelist <- lapply(ff, Personalized_Reader)

names(myfilelist) <- list.files(path="<add your dir path here>", full.names=FALSE)