I have the vector foo
:
> foo
983 984 985 986 987 988 989 990
cluster4 cluster4 cluster4 cluster1 cluster1 cluster1 cluster5 cluster5
Levels: cluster1 cluster4 cluster5
typeof(foo)
"integer"
class(foo)
"factor"
How could I remove the element "983"? So I get:
> foo_removed
984 985 986 987 988 989 990
cluster4 cluster4 cluster1 cluster1 cluster1 cluster5 cluster5
Levels: cluster1 cluster4 cluster5
We could use !is.na(as.numeric())
to identify the strings that are numeric and remove them.
onlynumbers <- "123.4"
onlyletters <- "abcd."
strings <- c(onlynumbers, onlyletters)
!is.na(as.numeric(strings))
[1] TRUE FALSE
As you can see this is working, now the removal
result <- strings[is.na(as.numeric(strings))]
> result
[1] "abcd."
EDIT You should first convert your factors to character using as.character.factor
and after you can reconvert using as.factor
EDIT 2 to keep the names you could use names(result) <- names(strings)[is.na(as.numeric(strings))]