The code below does the desired.
library(dplyr)
tab_mini <- head(tab)
tab_mini %>%
mutate(Cd_Disciplina_Simples = sub("^([[:alpha:]]*).*", "\\1", Cd_Disciplina)) %>%
mutate(Curso = recode(Cd_Disciplina_Simples,
ADF = "ADMINISTRACAO",
AGF = "AGRONOMIA",
BQF = "BIOQUIMICA")) %>%
select(-Cd_Disciplina_Simples)
Cd_Disciplina Nome_Disciplina Curso
1 ADF 401 SOCIOLOGIA RURAL ADMINISTRACAO
2 AGF 100 INTRODUÇÃO À AGRONOMIA AGRONOMIA
3 AGF 150 DESENHO TÉCNICO AGRONOMIA
4 BQF 100 BIOQUÍMICA FUNDAMENTAL BIOQUIMICA
5 BQF 101 LABORATÓRIO DE BIOQUÍMICA I BIOQUIMICA
6 BQF 102 BIOQUÍMICA BÁSICA BIOQUIMICA
I just applied it to the first six lines of the original dataset because they’re very different codes. I imagine that my example is enough to continue what is desired.
What my code does is this:
Through a regular expression, I create a column called Cd_Disciplina_Simples
. How the codes of the disciplines are of the type ABC XYZ
, only the part ABC
is required to determine the course. Thus, the regular expression I put there serves precisely to extract only the letters of the code of the discipline.
The function recode
is applied in Cd_Disciplina_Simples
just to make the conversion requested in the original question: ADF
flipped ADMINISTRACAO
, for example. Like the R
does not know what each of the codes of disciplines with three letters means, it is necessary to enter with their meaning manually.
Like the column Cd_Disciplina_Simples
is not necessary at the end, the function select
removes it from the final dataset. If there is a conflict between functions select
of different packages, replace the line
select(-Cd_Disciplina_Simples)
for
dplyr::select(-Cd_Disciplina_Simples)
So the R
will be informed that he must use the function select
package dplyr
.
Thanks Marcus Nunes, solved!!!
– fsbmat