使用多个if条件

时间:2012-11-29 12:26:56

标签: r if-statement for-loop

在对数据进行一些操作之后,我想自动重命名从字符串中获取名称的数据,合并这些部分然后分配给数据。我尝试在for循环中使用“if”函数,但代码不起作用。我尝试在“if”函数中使用“grep”作为条件。

    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="")

结果是:

Errore in if (grep("W_1", f2)) { : l'argomento ha lunghezza zero

1 个答案:

答案 0 :(得分:2)

没有任何for循环或if语句,在我看来,您可以简单地对所有内容进行矢量化:

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="")

您还可以简化以下方式:

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="")

如果你愿意,你可以包装一个函数:

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="")
    }