Home > Software engineering >  Dataframe with ranks into data frame with ordered list
Dataframe with ranks into data frame with ordered list

Time:01-28

I have this data frame which list the citation ranks of each variety by three people:

variety <- c("variety1","variety2","variety3","variety4","variety5")
people1 <- c(1, NA, 3, 2, NA)
people2 <- c(4, 3, 2, 1, NA)
people3 <- c(3, 2, 5, 4, 1)
df <- data.frame(variety, people1, people2, people3); df

I want a list or a dataframe like this:

people1 <- c("variety1", "variety3", "variety4",NA,NA)
people2 <- c("variety4", "variety3", "variety2", "variety1", NA)
people3 <- c("variety5", "variety2", "variety1", "variety4", "variety3")
df2 <- data.frame(people1, people2, people3); df2

So each column refers to a someone (people). And it lists the variety names in the order (rank) given in the input df.

Thanks,

CodePudding user response:

The functions pivot_wider and pivot_longer are really useful for restructuring data.

library(tidyverse)

df %>% 
    pivot_longer(
        cols = people1:people3,
        names_to = 'person',
        values_to = 'rank'
    ) %>% 
    filter(!is.na(rank)) %>% 
    pivot_wider(
        id_cols = rank,
        names_from = 'person', 
        values_from = 'variety'
    ) %>% 
    arrange(rank)

# A tibble: 5 × 4
   rank people1  people2  people3 
  <dbl> <chr>    <chr>    <chr>   
1     1 variety1 variety4 variety5
2     2 variety4 variety3 variety2
3     3 variety3 variety2 variety1
4     4 NA       variety1 variety4
5     5 NA       NA       variety3
  •  Tags:  
  • r
  • Related