Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
R 使用循环写入序列化文件_R_Serialization_For Loop_Write.table - Fatal编程技术网

R 使用循环写入序列化文件

R 使用循环写入序列化文件,r,serialization,for-loop,write.table,R,Serialization,For Loop,Write.table,我正在编写一个函数,该函数在dir()中打开所有文件,进行一些计算和重新排列,然后在dir()中再次写入(write.table)每个文件 代码看起来是这样的,除了循环不工作外,它做的一切都很好 mysub <- function(x) {sub(",",".",x)} abrir<-function(etapa){ ### volnum c(1:10) indica en qué vol quiero, ### etapa c("p","r","s") indica en qu

我正在编写一个函数,该函数在dir()中打开所有文件,进行一些计算和重新排列,然后在dir()中再次写入(write.table)每个文件

代码看起来是这样的,除了循环不工作外,它做的一切都很好

mysub <- function(x) {sub(",",".",x)}
abrir<-function(etapa){  
### volnum c(1:10) indica en qué vol quiero,
### etapa c("p","r","s") indica en qué etapa es del experimento

i<-1
for (i in dir()){

##### ABRIR ARCHIVOS DESDE DIR() ######
Archivo<-paste(getwd(),"/",i,sep="")
r1<-read.table(Archivo,fill=T,sep=",")

##### CALCULAR LA VELOCIDAD DE CADA CORRIDA ######
r11 <- (apply(r1, 2, mysub ))
r1<- data.frame(apply(r11, 2, as.numeric)) ## ojo que pueden aparecer NA
R1<-r1[1:3136,]     ### OJO, DEPENDE LA CORRIDA, SACA LOS VALORES DEL FINAL QUE NO SIRVEN
V<-data.frame(rbind(c(0,0,0,0),(abs(diff(as.matrix(R1[,2:5]))))))
Tiempo<-R1[,1]
Velocidad<-data.frame(Tiempo,V)  

##### GUARDAR LAS VELOCIDADES QUE CORRESPONDEN  ######

if (grep("p",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
} else if (grep("r",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
}else if(grep("s",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
}else stop("etapa inválida")
i<-i+1
}
}

mysub错误消息与以下事实有关:循环中的
i
字符
对象,而不是
数字
。(i in dir())
的语法
生成
i
中的实际文件,而不是索引。像这样使用
for
时,不需要将
i
设置为下一项,for
循环将采用该项。所以只要删除
就行了,谢谢。我明白你的意思。我也不喜欢for循环,但我认为这是唯一的方法。现在,尽管整个v2pi都运行,但循环在if中停止返回此错误(grep(“p”,i)=TRUE){:参数的长度为零。我使用grep是因为它匹配一个字符,例如我的iForget it,我耗尽了if语句,工作得很好,谢谢!!
mysub <- function(x) {sub(",",".",x)}
abrir<-function(etapa){  
### volnum c(1:10) indica en qué vol quiero,
### etapa c("p","r","s") indica en qué etapa es del experimento

i<-1
for (i in dir()){

##### ABRIR ARCHIVOS DESDE DIR() ######
Archivo<-paste(getwd(),"/",i,sep="")
r1<-read.table(Archivo,fill=T,sep=",")

##### CALCULAR LA VELOCIDAD DE CADA CORRIDA ######
r11 <- (apply(r1, 2, mysub ))
r1<- data.frame(apply(r11, 2, as.numeric)) ## ojo que pueden aparecer NA
R1<-r1[1:3136,]     ### OJO, DEPENDE LA CORRIDA, SACA LOS VALORES DEL FINAL QUE NO SIRVEN
V<-data.frame(rbind(c(0,0,0,0),(abs(diff(as.matrix(R1[,2:5]))))))
Tiempo<-R1[,1]
Velocidad<-data.frame(Tiempo,V)  

##### GUARDAR LAS VELOCIDADES QUE CORRESPONDEN  ######

if (grep("p",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
} else if (grep("r",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
}else if(grep("s",i)==TRUE){
salida<-paste("VEL",i,".txt",sep="")
write.table(Velocidad,salida,sep="\t",row.names=FALSE)
}else stop("etapa inválida")
i<-i+1
}
}
list_of_files = list.files('.')
do_some_calculations = function(file, output_name) {
    output_name = sprintf('VEL%d.txt', list_of_files)
    Archivo<-paste(getwd(), "/", i, sep = "")
    r1<-read.table(Archivo,fill = T, sep = ",")
    r11 <- (apply(r1, 2, mysub ))
    r1<- data.frame(apply(r11, 2, as.numeric)) ## ojo que pueden aparecer NA
    R1<-r1[1:3136,]     ### OJO, DEPENDE LA CORRIDA, SACA LOS VALORES DEL FINAL QUE NO SIRVEN
    V <- data.frame(rbind(c(0,0,0,0), (abs(diff(as.matrix(R1[,2:5]))))))
    Tiempo <- R1[,1]
    Velocidad <- data.frame(Tiempo, V)  
    write.table(Velocidad, output_name,s ep="\t", row.names=FALSE)
}