Hello R community, I need a bit of direction. I am new to R, so please forgive any mistakes or confusion. Here is an example of the columns and data contained in my data frame. Number: Factor w 345196 levels "1.001", "1.002", "1.003", "2.001", "2.002", "2.003" Name: factor w 2 levels ("X", "Y") Variable: factor w 21 levels "unknown", "known" Amount: num(1, 2, 3, 4, 5, 6) My objective is to filter based on numbers ending in .002 within column Number I have used the melt function to organize the data and then tried filter(df, PO.Number %in% c(.002)) However, this didn't work. Thanks for the help. [[alternative HTML version deleted]]
Hi, First of all, you almost certainly want to get your data into R with stringsAsFactors=FALSE rather than creating a factor with 345196 levels. mydata <- read.table("whateverfile", stringsAsFactors=FALSE) I'm just guessing, but you probably also don't really want filter(), but subset() instead. # fake data - you can use dput(head(mydata)) to provide some real data mydata <- data.frame(Number=c("1.001", "1.002", "1.003", "2.001", "2.002", "2.003"), Name=c("x", "y"), Amount=1:6, stringsAsFactors=FALSE) subset(mydata, grepl("\\.002$", mydata$Number)) Number Name Amount 2 1.002 y 2 5 2.002 x 5 If that's what you're after, you should probably take a look at ?filter to see what you were doing, and ?subset and ?grepl to see one way to approach the question. Sarah On Tue, Oct 13, 2015 at 2:24 PM, Clayton Samples <claytonsamples at gmail.com> wrote:> Hello R community, > > I need a bit of direction. I am new to R, so please forgive any mistakes or > confusion. > > > Here is an example of the columns and data contained in my data frame. > Number: Factor w 345196 levels "1.001", "1.002", "1.003", "2.001", "2.002", > "2.003" > Name: factor w 2 levels ("X", "Y") > Variable: factor w 21 levels "unknown", "known" > Amount: num(1, 2, 3, 4, 5, 6) > > My objective is to filter based on numbers ending in .002 within column > Number > > I have used the melt function to organize the data and then tried > > filter(df, PO.Number %in% c(.002)) > > However, this didn't work. > > Thanks for the help. >-- Sarah Goslee http://www.functionaldiversity.org
In general you should read the Posting Guide mentioned at the bottom of every email, which points out that you should post in plain text. Figuring out how to send plain text email in your email client will help you make sure that we see what you see. You should also try to insert reproducible code in your email rather than isolated snippets. Read the discussion at [1] to learn more. filter(df, grepl( "\\.002", as.character( Number ) ) ) If you import the data with the stringsAsFactors=TRUE option you could get rid of the as.character conversion. You should also look at the help for %in% because it compares the whole string, not sub strings. ?"%in%" [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On October 13, 2015 11:24:34 AM PDT, Clayton Samples <claytonsamples at gmail.com> wrote:>Hello R community, > >I need a bit of direction. I am new to R, so please forgive any >mistakes or >confusion. > > >Here is an example of the columns and data contained in my data frame. >Number: Factor w 345196 levels "1.001", "1.002", "1.003", "2.001", >"2.002", >"2.003" >Name: factor w 2 levels ("X", "Y") >Variable: factor w 21 levels "unknown", "known" >Amount: num(1, 2, 3, 4, 5, 6) > >My objective is to filter based on numbers ending in .002 within column >Number > >I have used the melt function to organize the data and then tried > >filter(df, PO.Number %in% c(.002)) > >However, this didn't work. > >Thanks for the help. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.