Gregg Powell
2021-Sep-15 02:01 UTC
[R] How to remove all rows that have a numeric in the first (or any) column
> 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.> > Data looks like the attached screen grab - >> 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-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 509 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20210915/f0da5667/attachment.sig>
Jeff Newmiller
2021-Sep-15 03:32 UTC
[R] How to remove all rows that have a numeric in the first (or any) column
An atomic column of data by design has exactly one mode, so if _any_ values are non-numeric then the entire column will be non-numeric. What does str(VPN_Sheet1$HVA) tell you? It is likely either a factor or character data. On September 14, 2021 7:01:53 PM PDT, Gregg Powell via R-help <r-help at 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. > >> >> Data looks like the attached screen grab - >> > >> 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-- Sent from my phone. Please excuse my brevity.
Rolf Turner
2021-Sep-15 03:47 UTC
[R] How to remove all rows that have a numeric in the first (or any) column
On Wed, 15 Sep 2021 02:01:53 +0000 Gregg Powell via R-help <r-help at 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, AZIf 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