user2961410
user2961410

Reputation: 73

Why is my loop in r is just running once?

I asked a very similar question before but the answers i got dont seem to apply in this case. The aim of my code is primarily to take a file, manipulate it and the save the manipulated file over the old file. Unfortunately there are a lot of file so I have incorporated a for loop but it is stopping after just one run through the loop. I think my return function is in the right place and my for statement worked in a previous slightly different version of the script. Here is my code:

AddLatLon<- function(num, condition){


#Set working directiory 
# num is the number of files needing extraction e.g (3:5) , c(2,7)

connect <- file("./EOBS DATA/sources.txt", "r")
locdata <- readLines(connect)
close(connect)

info <- locdata[24:length(locdata)] # removes non data before the data begins



Infodata <- read.table(text = info, sep=',',fill=TRUE,colClasses='character',header ==TRUE )
InfoTable <- read.csv("./EOBS DATA/sources.csv")  
InfoTable$STAID <- as.numeric(InfoTable$STAID)



for(i in c(num)){
filename <-paste("./EOBS DATA/",condition, "_csv_data/", condition,i, ".csv", sep = "")


#if(i <10){
  #filename <- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID00000", i, ".txt", sep = "")
#}
#if(i >=10 & i < 100){
  #filename <- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID0000", i, ".txt", sep = "")
#}

#if(i>= 100 & i <1000){
  #filename <- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID000", i, ".txt", sep = "")
#}
#if(i>= 1000){
  #filename <- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID00", i, ".txt", sep = "")
#}
if(file.exists(filename) == FALSE) {
  next
}


#con <- file(filename, "r")
#data <- readLines(con)
#close(con)
#q <- data[21:length(data)] # removes non data before the data begins



#Impactdata <- read.table(text = q, sep=',',fill=TRUE,colClasses='character',header = TRUE )


x <- read.csv(filename)



point <- match(i, InfoTable$STAID) 
Lat <- InfoTable[point,5]
Lon <- InfoTable[point,6]

Lat <- as.character(Lat)
Lon <- as.character(Lon)


x$Lat <- Lat
x$Lon <- Lon   



x$X <- NULL
x$DATE<- as.Date(as.character(x$DATE), format='%Y%m%d')

Savename <- paste("./EOBS DATA/",condition, "_csv_data/", condition,i, ".csv", sep = "")   

if(condition == "rr"){
  condition <- "Precipitation"
}
if(condition == "tn"){
  condition <- "Minimum Temperature"
}
if(condition == "tx"){
  condition <- "Maximum Temperature"
}

names(x)<- c("Station_ID", "Source_ID", "Date(yyyy-mm-dd)", condition, "Quality_code(0='valid'; 1='suspect')", "Latitude", "Longitude")


write.csv(x, Savename)


} 
return(head(x))
}

Upvotes: 0

Views: 4468

Answers (1)

flodel
flodel

Reputation: 89057

num is not defined, but from the name I'm pretty sure you want to be looping over 1:num, not c(num). So just replace:

for(i in c(num)){

with

for(i in 1:num)){

or

for(i in seq_len(num)){

Why seq_len? It will do the right thing if num is zero (no looping) or negative (throw an error).

Upvotes: 4

Related Questions