mardi 22 décembre 2015

Suggestions for speeding up double for loop with ifelse statement in r?

I have the following code, which produces what I want, but I'm almost certain that it can be written better (also it takes a long time to execute). Any suggestions for how I can clean it up?

The basic idea is that I want a table that includes the "highest" letter (A, B or C) in the variable var3 for each unique combination of var1 og var2

var1 <- c(rep.int(1:3,3))
var2 <- c(sample(letters[1:3],
                 NROW(var1),
                 replace = TRUE))
var3 <- c(sample(LETTERS[1:3],
                 NROW(var1),
                 replace = TRUE))
df <- data.frame(var1,
                 var2,
                 var3)
x <- capture.output(for (i in unique(df$var1)) {
  for (j in unique(df$var2)) {
    ifelse(
      "A" %in% subset(df,
                      df$var1 == i &
                        df$var2 == j,
                      select = c(var3))[,1]
      ,print("A"),
      ifelse(
        "B" %in% subset(df,
                        df$var1 == i &
                          df$var2 == j,
                        select = c(var3))[,1],
        print("B"),
        ifelse(
          "C" %in% subset(df,
                          df$var1 == i &
                            df$var2 == j,
                          select = c(var3))[,1],
          print("C"),0
        )
      )
    )
  }
})
as.data.frame(cbind(x,unique(paste(df$var1,df$var2))))

Aucun commentaire:

Enregistrer un commentaire