Reputation: 8247
I have following dataframe in R
ID Village_Name
1 23
2 Name-23
3 34
4 Vasai2
5 23
I only want to remove numbers from Village_Name, my desired dataframe would be
ID Village_Name
1 Name-23
2 Vasai2
How can I do it in R?
Upvotes: 2
Views: 228
Reputation: 887951
We can use grepl
to match one or more numbers from the start (^
) till the end ($
) of the numbers and negate (!
) it so that all numbers only elements become FALSE and others TRUE
i1 <- !grepl("^[0-9]+$", df1$Village_Name)
df1[i1, ]
Based on the OP's post, it could be also
data.frame(ID = head(df1$ID, sum(i1)), Village_Name = df1$Village_Name[i1])
# ID Village_Name
#1 1 Name-23
#2 2 Vasai2
Or another option is to convert to numeric resulting in non-numeric elements to be NA and is changed to a logical vector with is.na
df1[is.na(as.numeric(df1$Village_Name)),]
Upvotes: 4
Reputation: 522762
Here is another option using sub
:
df1[nchar(sub("\\d+", "", df1$Village_Name)) > 0, ]
The basic idea is to strip off all digits from the Village_Name
column, then assert that there is at least one character remaining, which would imply that the entry is not entirely numerical.
But, I would probably go with the grepl
option given by @akrun in practice.
Upvotes: 2