Return the first position

I have a problem, I want to create a new column that returns the first position where the number exceeds the column. How can I do it?

A B C D E Mean Pos
2 5 4 4 5 4 2
3 4 5 6 7 5 4
9 8 7 6 5 7 1

#risultati <- vector()
bjibl <- function (x){

for (i in 1:3){
for (j in 1:3600){

  if (x[i,j]>x[i, 3601]) {
    #risultati <- append(i, j, after = i)
    #risultati[i] <- j
    x1 <- as.data.frame(x[,1:j])
    x$risultati <- ncol(x1)
    i <- i +1 
    j <- 1
  }else{
    j<-j+1
  } 
} 
i <- i +1 

}
}

bjibl(db_class)

Here is one method using the dplyr library.

Df <- data.frame(A=c(2,3,9),B=c(5,4,8),C=c(4,5,7),D=c(4,6,6),E=c(5,7,7),Mean=c(4,5,7))
Df
#>   A B C D E Mean
#> 1 2 5 4 4 5    4
#> 2 3 4 5 6 7    5
#> 3 9 8 7 6 7    7
library(dplyr,warn.conflicts = FALSE)
Df %>% rowwise() %>% mutate(Pos = which(c_across(A:E) > Mean)[1])
#> # A tibble: 3 x 7
#> # Rowwise: 
#>       A     B     C     D     E  Mean   Pos
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
#> 1     2     5     4     4     5     4     2
#> 2     3     4     5     6     7     5     4
#> 3     9     8     7     6     7     7     1

Created on 2020-12-09 by the reprex package (v0.3.0)

In my program this code doesn't work. Maybe because my dataset is huge.

I use this code but it takes too long.

db_class %>% rowwise() %>% mutate(Pos = which(c_across (1:3600) > db_class$TREDEV_STD)[1])

The errors are due to the changes you made to the code. Using the data from my previous post, compare the result of these two lines.

Df %>% rowwise() %>% mutate(Pos=which(c_across(A:E) > Df$Mean)[1]) #error!
Df %>% rowwise() %>% mutate(Pos=which(c_across(A:E) > Mean)[1]) # no error

The mutate function is written so that columns are referred to by their bare names without the name of the data frame and $.

1 Like

Could you please explain why do we have to use [1] in this command and what does it mean in here, local context ? I am beginner and what I know is that mutate is used for creating new column. Is this ([1]) some kind of subsetting ?

Df %>% rowwise() %>% mutate(Pos=which(c_across(A:E) > Df$Mean)[1])

Yes, the square brackets are for subsetting the result returned by which(). The which() function takes a comparison and returns the indices where the comparison is true. In the example below, the comparison is x > 4 and it is true in the second and fifth positions. The which() function returns a vector with those two values. The square brackets can then be used to select from the vector that which() returns.

 x <- c(2, 5, 4, 4, 5)
 which(x > 4)
[1] 2 5

 which(x > 4)[1]
[1] 2

In the mutate() function in my earlier post, this method is used to assign to the Pos column the position of the first column that is greater than the Mean column.

Thank you very much indeed @FJCC, for detailed explanation, that helped me a lot.
best regards,
Andrzej

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.