3

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
7
  • 1
    You need to use ifelse, not if Commented Nov 29, 2012 at 12:31
  • 2
    Your code is not reproducible, because we don't know what file path is and what happens in those ...... I would argue for a solution involving switch Commented Nov 29, 2012 at 12:38
  • 2
    The specific cause of the error is that if statements should result in either TRUE or FALSE: if f2 indeed contains the string "W_1" then grep("W_1",f2) is equal to 1 (which is equivalent to TRUE so it works) but if f2 does not contains "W_1" then grep("W_1",f2) gives integer(0) which produces an error with if. Commented Nov 29, 2012 at 12:47
  • 3
    Following on from @plannapus' comment; you use grepl in some places and grep in others. It looks like you should be using grepl throughout ... Commented Nov 29, 2012 at 13:53
  • 1
    another problem is that unless there's something you're not showing us, your substr commands are indexing way outside of the strings -- those strings are only 59 characters long, while you're selecting positions 110-129 etc ... thus your substring variables will always be blank ... Commented Nov 29, 2012 at 14:40

1 Answer 1

2

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="")
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank a lot..!! the last question: Is it possible to generalized it? Is it possible to define some function to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.