Using Marcus's answer, I want to draw attention to something that often goes unnoticed to "new" users of R
, which is the data variable $ Name be of class factor
. This is important in the final result, after eliminating the values that interest the levels ( levels
) of the variable are still there. See yourself with code:
dados2 <- dados[grep("Silva", dados$Nome), ]
str(dados2)
'data.frame': 2 obs. of 2 variables:
$ Nome: Factor w/ 5 levels "Ana Silva","Isabela Cabral",..: 3 1
$ Nota: num 9 6
dados2$Nome
[1] João Silva Ana Silva
Levels: Ana Silva Isabela Cabral João Silva Paulo Santos Pedro Souza
If you want to delete these levels you can use the droplevels
function.
dados2$Nome <- droplevels(dados2$Nome)
dados2$Nome
[1] João Silva Ana Silva
Levels: Ana Silva João Silva
The other solution will be to start soon, when creating data.frame
dados
, use the stringsAsFactors
argument.
dados <- data.frame(Nome=c("João Silva", "Pedro Souza", "Ana Silva",
"Isabela Cabral", "Paulo Santos"), Nota=c(9, 8, 6, 10, 5),
stringsAsFactors = FALSE) ## Aqui, por defeito é TRUE
Then just use Marcus's solution.