[R] How to remove all rows that have a numeric in the first (or any) column

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Wed Sep 15 05:47:32 CEST 2021


On Wed, 15 Sep 2021 02:01:53 +0000
Gregg Powell via R-help <r-help using r-project.org> wrote:

> > Stuck on this problem - How does one remove all rows in a dataframe
> > that have a numeric in the first (or any) column?
> > 
> 
> > Seems straight forward - but I'm having trouble.
> > 
> 
> 
> I've attempted to used:
> 
> VPN_Sheet1 <- VPN_Sheet1[!is.numeric(VPN_Sheet1$HVA),]
> 
> and
> 
> VPN_Sheet1 <- VPN_Sheet1[!is.integer(VPN_Sheet1$HVA),]
> 
> Neither work - Neither throw an error.
> 
> class(VPN_Sheet1$HVA)  returns:
> [1] "list"
> 
> So, the HVA column returns a list.

Do you mean that the HVA column *is* a list? It probably shouldn't be.
It seems very likely that your data are all screwed up.  The first
thing to do is get your data properly organised.  That could be
difficult since you have apparently read them in from an Excel file,
and Excel is a recipe for disaster.

> 
> >
> > Data looks like the attached screen grab -

No attachment came through.  Do read the posting guide.  Most
attachments are stripped by the mail handler.

> > The ONLY rows I need to delete are the rows where there is a
> > numeric in the HVA column.
> > 
> 
> > There are some 5000+ rows in the actual data.
> > 
> 
> > Would be grateful for a solution to this problem.
> 
> How to get R to detect whether the value in column 1 is a number so
> the rows with the number values can be deleted?
> > 
> 
> > Thanks in advance to any and all willing to help on this problem.
> > 
> 
> > Gregg Powell
> > 
> 
> > Sierra Vista, AZ

If there are any non-numeric entries in a column then they *all* have
to be non-numeric.  Some of them *may* be interpretable as being
numeric.

If you apply as.numeric() to a column you'll get NA's for all entries
that *cannot* be interpreted as numeric. So you may want to do something
like (untested, of course):

ok <- is.na(as.numeric(X[,"HVA"]))
X  <- X[ok,]

where "X" is the data frame that you are dealing with.

Good luck.

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276



More information about the R-help mailing list