0
我寫打開的所有文件的dir()的函數文件,做了一些計算和重排,然後再寫入(write.table)的每個文件的dir()寫作系列化與循環
dir()
[1] "v2p1" "v2p10" "v2p11" "v2p12" "v2p2" "v2p3" "v2p4" "v2p5" "v2p6" "v2p7"
[11] "v2p8" "v2p9" "v2r1" "v2r10" "v2r11" "v2r12" "v2r2" "v2r3" "v2r4" "v2r5"
[21] "v2r6" "v2r7" "v2r8" "v2r9" "v2s1" "v2s10" "v2s11" "v2s12" "v2s2" "v2s3"
[31] "v2s4" "v2s5" "v2s6" "v2s7" "v2s8" "v2s9"
代碼看起來像這樣,除了循環無法正常工作以外,它的一切都很好。
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
}
}
abrir()爲我節省了正確的「VELv2p1.txt」並返回此錯誤 錯誤在我+ 1:非數值參數二元運算
我不知道怎麼解釋[R繼續第二個元素一旦完成i = 1我想我< -i + 1它不是這樣。我會很感激你的幫助。
謝謝。我明白你的意思。我不喜歡for循環,但我認爲這是唯一的方法。現在循環遍歷整個v2pi並停止返回此錯誤錯誤if(grep(「p」,i)== TRUE){:參數的長度爲零。我使用了grep,因爲它的匹配包含一個字符,例如我的字符 –
忘記它,我耗盡了if語句並且工作正常,謝謝! –