0

So I'm cleaning up a huge data file in R and an example is as shown:

ID       Score
1001       4
1002       2
1003       h
1004       v
1005       3

Because the class of Score column is "character", I want to use the as.numeric function to convert 4,20 and 30 to numeric values. But since the data is dirty (contains unreasonable strings like h, v), I get the message:

NAs introduced by coercion.

When i run the following:

as.numeric(df$Score)

So what i want to do now is to remove the rows in the dataframe that contains strings with letters so that i would obtain:

ID       Score
1001       4
1002       2
1005       3
1

2 Answers 2

1

There are multiple ways you can do this :

Convert to numeric and remove NA values

subset(df, !is.na(as.numeric(Score)))

#    ID Score
#1 1001     4
#2 1002    20
#5 1005    30

Or with grepl find if there are any non-numeric characters in them and remove them

subset(df, !grepl('\\D', Score))

This can be done with grep as well.

df[grep('\\D', df$Score, invert = TRUE), ]

data

df <- structure(list(ID = 1001:1005, Score = c("4", "20", "h", "v", 
"30")), class = "data.frame", row.names = c(NA, -5L))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but the first solution still produces a message "In eval(e, x, parent.frame()) : NAs introduced by coercion". But the second one works without having the message generated.
@Astrox yes, because we are trying to change the character value to numeric there hence you get the warning message. In this case, it is safe to ignore it.
0

You may use the str_detect in the tidyverse package, as follows:

df[str_detect(df$Score, "\\d"),]

or

df %>% filter(str_detect(df$Score, "\\d"))

Both produce the output:

#    ID Score
#1 1001     4
#2 1002    20
#5 1005    30

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.