Applying a function to every combination of two columns in a dataframe using R

goclem

I want to apply a function myfooto every possible combination of two columns in a dataframe mydf and get the result in a matrix format myoutput.

Consider the following dataframe,

# Example dataframe
mydf <- data.frame(var1 = 1:10, var2 = 11:20, var3 = 21:30)
head(mydf)

# var1 var2 var3
# 1    11   21
# 2    12   22
# 3    13   23
# 4    14   24
# 5    15   25

to which I want to apply the following function to every possible combination of two columns,

# Example function
myfoo <- function(varA, varB) sum(varA * varB)

myfoo(var1, var2)
# [1] 935

in order to get this output.

# Desired output
myoutput <- matrix(c(0, 935, 1485, 935, 0, 4035, 1485, 4035, 0), 3, dimnames = list(names(mydf), names(mydf)))
myoutput

#      var1 var2 var3
# var1    0  935 1485
# var2  935    0 4035
# var3 1485 4035    0
David Arenburg

In your case I would convert to a matrix (no reason to keep it a data.frame when all columns are of numeric class) and just run the compiled crossprod function which does a matrix cross product.

m <- as.matrix(mydf)
res <- crossprod(m, m)
diag(res) <- 0 # You can probably skip that part
res
#      var1 var2 var3
# var1    0  935 1485
# var2  935    0 4035
# var3 1485 4035    0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Applying a function to every combination of two columns in a dataframe using R

From Dev

R: Applying a function to every element of a dataframe

From Dev

R: Applying a function to every entry in dataframe

From Dev

How to apply a function on every possible combination of two columns in two matrices in R

From Dev

Applying function to a DataFrame using its columns as parameters

From Dev

Applying a function to every combination of elements in a vector

From Dev

Applying a function to every combination of elements in a vector

From Dev

Apply function to every combination of columns

From Dev

Applying lambda function on two columns in a Dataframe to get Geolocation

From Dev

R - Perform function for every two columns in data

From Dev

Using arrayfun to apply two arguments of a function on every combination

From Dev

R applying function on a dataframe

From Dev

walking through two tables (matrices) matching columns and applying a function in R

From Dev

walking through two tables (matrices) matching columns and applying a function in R

From Dev

Applying a function on every 3 columns (cols: 1-3, 4-6, 7-9) in dataframe

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

Applying function to every other column in pandas dataframe

From Dev

Applying iterative function to every group in pandas DataFrame

From Dev

applying a function to a timeseries in a R dataframe

From Dev

How to apply specific function to range of columns(but applying it to every column alone) in R?

From Dev

Filter Pandas dataframe based on combination of two columns

From Dev

Applying function to dataframe columns spark scala

From Dev

Apply custom function to two columns for every row in data frame in R

From Java

Applying a function to every row of a table using dplyr?

From Dev

Aggregate function in R using two columns simultaneously

From Dev

generating matrix by applying function to all possible combination of variables in r

From Dev

pandas - automate graph using the combination of two columns

From Dev

Applying 'complex' function n times and record results using data from columns of two separate dataframes

From Dev

Applying function to every cell in a Dataframe based on index and col

Related Related

  1. 1

    Applying a function to every combination of two columns in a dataframe using R

  2. 2

    R: Applying a function to every element of a dataframe

  3. 3

    R: Applying a function to every entry in dataframe

  4. 4

    How to apply a function on every possible combination of two columns in two matrices in R

  5. 5

    Applying function to a DataFrame using its columns as parameters

  6. 6

    Applying a function to every combination of elements in a vector

  7. 7

    Applying a function to every combination of elements in a vector

  8. 8

    Apply function to every combination of columns

  9. 9

    Applying lambda function on two columns in a Dataframe to get Geolocation

  10. 10

    R - Perform function for every two columns in data

  11. 11

    Using arrayfun to apply two arguments of a function on every combination

  12. 12

    R applying function on a dataframe

  13. 13

    walking through two tables (matrices) matching columns and applying a function in R

  14. 14

    walking through two tables (matrices) matching columns and applying a function in R

  15. 15

    Applying a function on every 3 columns (cols: 1-3, 4-6, 7-9) in dataframe

  16. 16

    Applying iterative function to every group in pandas DataFrame

  17. 17

    Applying function to every other column in pandas dataframe

  18. 18

    Applying iterative function to every group in pandas DataFrame

  19. 19

    applying a function to a timeseries in a R dataframe

  20. 20

    How to apply specific function to range of columns(but applying it to every column alone) in R?

  21. 21

    Filter Pandas dataframe based on combination of two columns

  22. 22

    Applying function to dataframe columns spark scala

  23. 23

    Apply custom function to two columns for every row in data frame in R

  24. 24

    Applying a function to every row of a table using dplyr?

  25. 25

    Aggregate function in R using two columns simultaneously

  26. 26

    generating matrix by applying function to all possible combination of variables in r

  27. 27

    pandas - automate graph using the combination of two columns

  28. 28

    Applying 'complex' function n times and record results using data from columns of two separate dataframes

  29. 29

    Applying function to every cell in a Dataframe based on index and col

HotTag

Archive