0

I am trying to apply an operation which applies on each row of a dataframe. Below is the code.

  for(i in 1:nrow(df)){
    df$final[i] <- alignfile[alignfile$Response == df$value[i],]$Aligned
  }

It is basically doing the vlookup from "alignfile" data frame and making a new column with the successful vlookup of "value" column in data frame "df".

How do i replace this operation with apply family of function so that i can get rid of for loops which is making it slow.

Looking for suggestions. Please feel free for more clarifications.

Thanks

  • Are you looking for `findInterval()` ? Eventually `df$final <- findInterval(df$value, alignfile$Response)` or similar. – jogo Apr 12 '17 at 07:18
  • No, i am looking to create new column which matches the value of "value" column of df with "aligned" column of alignfile. It is basically vlookup. – Rishab Asthana Apr 12 '17 at 07:21
  • Please give a [mcve], i.e. edit your question: http://stackoverflow.com/posts/43362794/edit Read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example At the current state your question is off-topic for SO. – jogo Apr 12 '17 at 07:24
  • `alignfile$Aligned[findInterval(df$value, alignfile$Response)]` Please read the documentation of the function `findInterval()` for more information. – jogo Apr 12 '17 at 07:29
  • Here is an example: `SLclass <- data.frame(SL=c(3,4,5,6,7,8), C=c("XS", "S", "M", "L", "XL", "XXL")); SLclass$C[findInterval(iris$Sepal.Length, SLclass$SL)]` – jogo Apr 12 '17 at 07:48

1 Answers1

-1

You didn't provide a reproducible example so take my answer with a grain of salt, I think you don't need to use a for loop at all in this case (as in most cases with R) and neither an apply function. I think this problem could be easily solved with an ifelse in the following way:

df$final <- ifelse(alignfile$Response==df$value, 1, 0)

this will put a one in the final column of the df dataframe if the value in the current cell of the alignfile$Response column is equal to the value of the current cell in the df$value column. This assumes alignfile and df have the same number of rows (as it appears from the code you provided).

Gius Dep
  • 70
  • 8