5
library(tidyverse)
I have a dataset that has in one column titles of articles and in another column, their respective authors. I reproduce here a row of this dataset:
df<-tibble(
titulo= "A URBANIZAÇÃO NEOLIBERAL",
autores= "CLAUDIO; DIANA; MILENA")
df
# A tibble: 1 x 2
titulo autores
<chr> <chr>
1 A URBANIZAÇÃO NEOLIBERAL CLAUDIO; DIANA; MILENA
Then I divided the line above according to the authors using separate_rows()
df2<-df %>%
separate_rows(autores, sep = "; ")
df2
# A tibble: 3 x 2
titulo autores
<chr> <chr>
1 A URBANIZAÇÃO NEOLIBERAL CLAUDIO
2 A URBANIZAÇÃO NEOLIBERAL DIANA
3 A URBANIZAÇÃO NEOLIBERAL MILENA
Now I wanted each author to be identified according to their situation ("teacher", "student" or "graduate"). I have a spreadsheet with everything detailed. I reproduce here an excerpt:
corpo_programa <- tibble(nome = c("FULANO", "BELTRANO", "CLAUDIO", "MILENA", "DIANA"),
situacao = c("docente", "docente", "docente", "discente", "egresso"))
corpo_programa
# A tibble: 5 x 2
nome situacao
<chr> <chr>
1 FULANO docente
2 BELTRANO docente
3 CLAUDIO docente
4 MILENA discente
5 DIANA egresso
What I wanted was to use this spreadsheet as a kind of dictionary that compared with the column "authors" to generate me a column with the situation of each author.
I figured using the function setNames()
, I could.
dicionario<- setNames(corpo_programa$nome, corpo_programa$situacao)
Then I used mutate()
:
df2 %>%
mutate(condicao = dicionario[autores])
# A tibble: 3 x 3
titulo autores condicao
<chr> <chr> <chr>
1 A URBANIZAÇÃO NEOLIBERAL CLAUDIO NA
2 A URBANIZAÇÃO NEOLIBERAL DIANA NA
3 A URBANIZAÇÃO NEOLIBERAL MILENA NA
The newly created "condition" column appears with "NA", when I expected it to be filled with "teacher", "graduate" and "student".