Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function to dataframe with changing argument

Tags:

r

apply

I have 2 objects:

A data frame with 3 variables:

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

A numeric vector with 3 elements:

nv <- c(6,11,28)

I would like to compare the first variable to the first number, the second variable to the second number and so on.

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

Of course in reality my data frame has a lot more variables so manually typing each variable is not an option.

I encounter these kinds of problems quite frequently. What kind of documentation would I need to read to be fluent in these matters?

like image 201
1053Inator Avatar asked Jun 04 '15 19:06

1053Inator


People also ask

How do you replace a specific value in a data frame?

Pandas DataFrame replace() Method The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.

How do you apply a function in a data frame?

DataFrame - apply() function. The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).


2 Answers

One option would be to compare with equally sized elements. For this we can replicate the elements in 'nv' each by number of rows of 'df' (rep(nv, each=nrow(df))) and compare with df or use the col function that does similar output as rep.

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

If you need a logical matrix that corresponds to comparison of each column with each element of 'nv'

 sweep(df, 2, nv, FUN='>')
like image 199
akrun Avatar answered Sep 24 '22 00:09

akrun


You could 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
like image 34
sgibb Avatar answered Sep 24 '22 00:09

sgibb