Remove the rows that have non-numeric characters in one column in R

Or using @Brandon Bertelsen's example data

dat1 <- transform(dat[grep("^\\d+$", dat$A),,drop=F], A= as.numeric(as.character(A)))
dat1
#   A
#6  1
#7  2
#8  3
#9  4
#10 5

 str(dat1)
#'data.frame':  5 obs. of  1 variable:
#$ A: num  1 2 3 4 5

When you import data to a data.frame, it generally gets converted to a factor if the entire column is not numeric. With that in mind, you usually have to convert to character and then to numeric.

dat <- data.frame(A=c(letters[1:5],1:5))

str(dat)
'data.frame':   10 obs. of  1 variable:
 $ A: Factor w/ 10 levels "1","2","3","4",..: 6 7 8 9 10 1 2 3 4 5

as.numeric(as.character(dat$A))
 [1] NA NA NA NA NA  1  2  3  4  5
Warning message:
NAs introduced by coercion  

Notice that it converts characters to NA. Combining this:

dat <- dat[!is.na(as.numeric(as.character(dat$A))),]

In words, the rows of dat that are not NA after conversion from factor to numeric.

Second Issue:

> dat <- data.frame(A=c(letters[1:5],1:5))
> dat <- dat[!is.na(as.numeric(as.character(dat$A))),]
Warning message:
In `[.data.frame`(dat, !is.na(as.numeric(as.character(dat$A))),  :
  NAs introduced by coercion
> dat <- dat[!is.na(as.numeric(as.character(dat$A))),]
Error in dat$A : $ operator is invalid for atomic vectors

Tags:

R