Apply function to dataframe with variable argument

I have 2 objects:

Data frame with three variables:

v1 <- 1:10 v2 <- 11:20 v3 <- 21:30 df <- data.frame(v1,v2,v3) 

A numerical vector with three elements:

 nv <- c(6,11,28) 

I would like to compare the first variable with the first number, the second variable with the second number, etc.

 which(df$v1 > nv[1]) which(df$v2 > nv[2]) which(df$v3 > nv[3]) 

Of course, in fact, there are a lot more variables in my data frame, so manually setting each variable is not an option.

I often encounter such problems. What documentation will I need to read in order to speak freely about it?

+5
source share
3 answers

One option is to compare with items of the same size. To do this, we can replicate the elements in 'nv' each according to the number of rows 'df' ( rep(nv, each=nrow(df)) ) and compare with df or use the col function, which draws similar output as rep .

  which(df > nv[col(df)], arr.ind=TRUE) 

If you need a logical matrix corresponding to comparing each column with each element of 'nv'

  sweep(df, 2, nv, FUN='>') 
+8
source

You can also use mapply :

 mapply(FUN=function(x, y)which(x > y), x=df, y=nv) #$v1 #[1] 7 8 9 10 # #$v2 #[1] 2 3 4 5 6 7 8 9 10 # #$v3 #[1] 9 10 
+4
source

I think that such situations are complicated, because ordinary loopback solutions (for example, the apply function) go through only one object, but you need to scroll through df and nv at the same time. One approach is to iterate over the indices and use them to get the relevant information from both df and nv . A convenient way to scroll indexes is with the sapply function:

 sapply(seq_along(nv), function(x) which(df[,x] > nv[x])) # [[1]] # [1] 7 8 9 10 # # [[2]] # [1] 2 3 4 5 6 7 8 9 10 # # [[3]] # [1] 9 10 
+3
source

All Articles