2012-11-29 38 views
3

数据的一些处理之后,我想自动重命名数据取名字的作品从一个字符串,合并togheter这些片,然后分配给数据。我尝试在for循环中使用“if”函数,但代码不起作用。我试图在“if”函数中使用“grep”作为条件。使用多个if条件

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

您需要使用'ifelse',不'if' – Andrie

+2

你的代码是不可复制的,因为我们不知道是什么文件路径和那些'.....'发生了什么。我认为一个解决方案涉及'之开关 –

+2

错误的具体原因是'if'报表应导致TRUE或FALSE:如果'f2'确实包含字符串'“W_1”''然后grep的(“W_1 ”,F2)'是等于1(即相当于TRUE所以它的工作原理),但如果'f2'不包含' “W_1”''然后grep的( “W_1”,F2)''给出整数(0)'这会在if中产生一个错误。 – plannapus

回答

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

非常感谢..!最后一个问题:是否有可能推广它?是否可以定义一些功能来做到这一点? –