Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple if condition

After some manipulation of data, I want to rename automatically the data taking pieces of name from a string, merging togheter these pieces and then assigning to data. I try to use "if" function in for loop but the code doesn't work. I try to use "grep" as condition in "if" function.

    filepath<-c("C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_0.csv", 
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_1.csv", 
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_2.csv", 
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_3.csv",          
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_4.csv",
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_5.csv",
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_0-P1_6.csv",
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_1-P1_0.csv",
      "C:/Users/Amministratore/Documents/Isolante-T_0-W_1-P1_1.csv",
       ....)
    for(i in filepath){
    ......
    f <- substr(i,10,23)                  # first piece of name
    f2 <- as.character(substr(i,40,57))   # second piece of name

    if (grep("W_0",f2)){
        m<-c("_sin")
    }
    if (grep("W_1",f2)){
        m<-c("_jan2_febreal")
    }
    if (grep("W_2",f2)){
        m<-c("_real")
    }
    if (grep("W_3",f2)){
        m<-c("_step")
    }

    if (grep("P1_0",f2,value = FALSE)){
        t<-c("_t0.025")
    }
    if (grepl("P1_1",f2,value = FALSE)){
        t<-c("_t0.05")
    }
    if (grepl("P1_2",f2,value = FALSE)){
        t<-c("_t0.1")
    }
    if (grepl("P1_3",f2,value = FALSE)){
        t<-c("_t0.15")  
    }
    if (grepl("P1_4",f2,value = FALSE)){
        t<-c("_t0.2")
    }
    if (grepl("P1_5",f2,value = FALSE)){
        t<-c("_t0.25")
    }
    if (grepl("P1_6",f2,value = FALSE)){
        t<-c("_t0.3")
    }
}
Outputfilename <- paste(paste(f,m,sep=""),t,sep="")

the result is:

Errore in if (grep("W_1", f2)) { : l'argomento ha lunghezza zero
like image 483
Marco Giuliani Avatar asked Jul 17 '26 18:07

Marco Giuliani


1 Answers

Without any for loops or if statements, it seems to me you can simply vectorize everything:

f <- substr(filepath,10,23)

m <- t <- character(length(filepath))

m[grepl("W_0",filepath)]<-"_sin"
m[grepl("W_1",filepath)]<-"_jan2_febral"
m[grepl("W_2",filepath)]<-"_real"
m[grepl("W_3",filepath)]<-"_step"

t[grepl("P1_0",filepath)]<-"_t0.025"
t[grepl("P1_1",filepath)]<-"_t0.05"
t[grepl("P1_2",filepath)]<-"_t0.1"
t[grepl("P1_3",filepath)]<-"_t0.15"
t[grepl("P1_4",filepath)]<-"_t0.2"
t[grepl("P1_5",filepath)]<-"_t0.25"
t[grepl("P1_6",filepath)]<-"_t0.3"

Outputfilename <- paste(f,m,t,sep="")

That you can also probably simplify the following way:

f <- substr(filepath,10,23)

m <- t <- character(length(filepath))

w <- array(c(paste("W",0:3,sep="_"),
             "_sin", "_jan2_febral", "_real", "_step"), dim=c(4,2))
p <- array(c(paste("P1",0:6,sep="_"),    
           paste("t_0.",c("025","05","1","15","2","25","3"),sep="")), dim=c(7,2))

for(i in 1:nrow(w)){m[grepl(w[i,1],filepath)] <- w[i,2]}
for(i in 1:nrow(p)){t[grepl(p[i,1],filepath)] <- p[i,2]}
Outputfilename <- paste(f,m,t,sep="")

That you can wrap in a function if you like:

outputfile.namer <- function(filepath,w,p){
    # filepath being your vector of file paths
    # w and p your correspondance tables for your "W_" and "P_" series respectively

    f <- do.call(rbind,strsplit(gsub("C:/Users/","",filepath),split="/"))[,1]
    # the preceding is more general than `f <- substr(filepath,10,23)` to grab the name of the User
    m <- t <- character(length(filepath))
    for(i in 1:nrow(w)){m[grepl(w[i,1],filepath)] <- w[i,2]}
    for(i in 1:nrow(p)){t[grepl(p[i,1],filepath)] <- p[i,2]}
    paste(f,m,t,sep="")
    }
like image 75
plannapus Avatar answered Jul 20 '26 13:07

plannapus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!