0

I would like to create a tibble which looks like this:

# A tibble: 3 x 4
  team    arsenal      chelsea spurs       
  <chr>   <chr>        <chr>   <chr>       
1 arsenal self         london  north-london
2 chelsea london       self    london      
3 spurs   north-london london  self  

As you can see, the information in the tibble is duplicated. Reading along the first row (team = arsenal), we can see there is a 'north-london' derby between 'arsenal' and 'spurs'. Similarly, reading along the third row (team = spurs), there is a 'north-london' derby between 'spurs' and 'arsenal'.

Let's call this tibble df_derbies. I created it with the following code:

library(tidyverse)

## create vectors
team <- c("arsenal", "chelsea", "spurs")
arsenal <- c("self", "london", "north-london")
chelsea <- c("london", "self", "london")
spurs <- c("north-london", "london", "self")

## combine vectors into dataframe
df_derbies <- tibble(team, arsenal, chelsea, spurs)
df_derbies

My question is twofold:

1) Is there a way to create the initial vectors so that I do not have to type out duplicate information? This will mean I only have to type 'north-london' once, for example.

2) After the first step, is there a function(s) that can create a tibble as above? This will essentially duplicate the information for the relevant combination of row and column.

The reason for doing so is that I would like to create a larger tibble, with up to 20 rows. I am open to suggestions for a better way to create and combine the vectors!

aaaakkkkk
  • 80
  • 5

2 Answers2

1

You can use a matrix and use upper.tri and lower.tri function of basic R like this :

## create vectors
team <- c("arsenal", "chelsea", "spurs")
arsenal <- c("self", "london", "north-london")
chelsea <- c("", "self", "london")
spurs <- c("", "", "self")

## combine vectors into dataframe
df_derbies <- rbind(arsenal, chelsea, spurs)
rownames(df_derbies) <- c("arsenal", "chelsea", "spurs")
colnames(df_derbies) <- c("arsenal", "chelsea", "spurs")
df_derbies[lower.tri(df_derbies)] <- df_derbies[upper.tri(df_derbies)]
Rémi Coulaud
  • 1,684
  • 1
  • 8
  • 19
1

To avoid typing duplicate information you will need to first work with a matrix and then convert the matrix to a tibble. Here is one way to do so:

library(tibble)

teams <- c("arsenal", "chelsea", "spurs")
derbies <- c("london", "north-london", "london")
mx <- matrix("self", length(teams), length(teams))
mx[lower.tri(mx)] <- mx[upper.tri(mx)] <- derbies
df_derbies <- as_tibble(cbind(teams, mx), .name_repair = function(x) c("teams", teams))
the-mad-statter
  • 5,650
  • 1
  • 10
  • 20