Using Marcus' answer, I just want to draw attention to something that often goes unnoticed by "new" users of R
, which is the variable given$Name being of class factor
. This has importance in the final result, after eliminating the values that matter the levels (levels
) variable are still there. See 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 function droplevels
.
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 by creating the data.frame
dados
, use the argument stringsAsFactors
.
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' solution.