3

データを操作した後、文字列から名前の一部を取得し、これらの部分をマージしてからデータに割り当てて、データの名前を自動的に変更したいと考えています。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
4

1 に答える 1

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="")
    }
于 2012-11-29T15:20:38.807 に答える