How to perform vector based function on a dataframe row

Hello experts,

I am new to R and I have this problem where I am trying to figure out how can I perform a vector based function on all the values in a row in a dataframe and implement that on the whole dataframe.

To elaborate, my dataframe is following

> Column1<-c("red","blue")#creating column1
> Column2<-c("green","white")#creating column2
> Column3<-c("aqua","magenta")#creating column2
> df_1<-data.frame(Column1, Column2, Column3)#creating data frame
> df_1

which produces following

| Column1 | Column2 | Column3 |
|---------|---------|---------|
| red     | green   | aqua    |
| blue    | white   | magenta |

Now, I want to run a combn on each of the row of this dataframe.

I know how to run combn on a single vector like following

> result1<-combn(c("red","green","aqua"),2)
> result2<-combn(c("blue","white","magenta"),2)

which gives me this
result1

| Column1 | Column2 | Column3 |
|---------|---------|---------|
| red     | red     | green   |
| green   | aqua    | aqua    |

result2

| Column1 | Column2 | Column3 |
|---------|---------|---------|
| blue    | blue    | white   |
| white   | magenta | magenta |

Now all I want to do is to run the combn on the rows of the previously created df_1 in such a way so that it gives me the following output in a dataframe

| Column1 | Column2 | Column3 | Result  | CAT     |
|---------|---------|---------|---------|---------|
| red     | green   | aqua    | red     | Result1 |
| red     | green   | aqua    | green   | Result1 |
| red     | green   | aqua    | red     | Result2 |
| red     | green   | aqua    | aqua    | Result2 |
| red     | green   | aqua    | green   | Result3 |
| red     | green   | aqua    | aqua    | Result3 |
| blue    | white   | magenta | blue    | Result1 |
| blue    | white   | magenta | white   | Result1 |
| blue    | white   | magenta | blue    | Result2 |
| blue    | white   | magenta | magenta | Result2 |
| blue    | white   | magenta | white   | Result3 |
| blue    | white   | magenta | magenta | Result3 |


Thank you in advance.

Column1<-c("red","blue")#creating column1
Column2<-c("green","white")#creating column2
Column3<-c("aqua","magenta")#creating column2
df_1<-data.frame(Column1, Column2, Column3)
library(dplyr,warn.conflicts = FALSE)
library(tidyr)

df_2 <- df_1 %>% rowwise() %>% 
  mutate(COMB=list(combn(c_across(Column1:Column3),2,simplify = FALSE))) %>% 
  unnest(cols = COMB) %>% 
  unnest(cols=COMB) %>% 
  group_by(Column1,Column2,Column3) %>% 
  mutate(CAT=paste("Result", ((row_number() - 1) %/% 2)+1))
df_2
#> # A tibble: 12 x 5
#> # Groups:   Column1, Column2, Column3 [2]
#>    Column1 Column2 Column3 COMB    CAT     
#>    <chr>   <chr>   <chr>   <chr>   <chr>   
#>  1 red     green   aqua    red     Result 1
#>  2 red     green   aqua    green   Result 1
#>  3 red     green   aqua    red     Result 2
#>  4 red     green   aqua    aqua    Result 2
#>  5 red     green   aqua    green   Result 3
#>  6 red     green   aqua    aqua    Result 3
#>  7 blue    white   magenta blue    Result 1
#>  8 blue    white   magenta white   Result 1
#>  9 blue    white   magenta blue    Result 2
#> 10 blue    white   magenta magenta Result 2
#> 11 blue    white   magenta white   Result 3
#> 12 blue    white   magenta magenta Result 3

Created on 2020-11-03 by the reprex package (v0.3.0)

1 Like

Many thanks @FJCC. I do have a followup question for you.

Let's suppose instead of performing combination through combn, I want to perform combination through a different package, let's suppose gtools

Now, in gtools I can do following

> library(gtools)
> x <- c('red', 'green', 'aqua')
> output<-combinations(n=3,r=2,v=x,repeats.allowed=F)
> output

which gives me result 1.

If I decided to use a different library how can I adapt your solution to that. Many thanks in advance for your time and help.

I do not have gtools but I think its output is the same as combn with simplify = TRUE. I cannot find a way to get that output into the form you want but I do not use nested structures. I hope someone more familiar with that can help out.

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.