83290

select row based on the condition of number apparence for first time

I would like to find rows in which defined number appear (for example 2) for first time?

For example:

group <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b") value <- c(1, 1, 2, 2, 1, 1, 2, 1, 2, 3) GOAL <- c("FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE") data <- data.frame(group, value, GOAL) data

In the column "GOAL" would be the result. Thank you for your help in advance.

Answer1:

This way assumes each group has at least one 2. Although your sample data is ordered by group, the approach used here doesn't depend on this.

# given vector v, return vector of FALSEs, except at the first 2 f <- function(v) replace(logical(length(v)), which(v == 2)[1], TRUE) transform(data, GOAL=as.logical(ave(value, group, FUN=f))) # group value GOAL # 1 a 1 FALSE # 2 a 1 FALSE # 3 a 2 TRUE # 4 a 2 FALSE # 5 a 1 FALSE # 6 b 1 FALSE # 7 b 2 TRUE # 8 b 1 FALSE # 9 b 2 FALSE # 10 b 3 FALSE

The call to as.logical is necessary if you want TRUE/FALSE, since ave always returns a numeric vector. Without as.logical, you get 0s and 1s.

Answer2:

Or you can just do

valueFirst <- 2 data[data$value==valueFirst,][1,]

Then you have the row. If you want only GOAL as output:

data[data$value==valueFirst,][1,3]

Recommend

  • Using nntool [MATLAB] from command line
  • How to implement laravel function Crypt::encrypt() in Objective C?
  • how to write block with arg. “UnsafeMutablePointer” in Swift closure
  • OpenMP for dependent variables
  • Rails 4 order by virtual attribute
  • trigger Event from iframe
  • Comma within fields in CSV file -import to DB using SSIS
  • Aspect advising other aspects
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Defining variable by logical subseting on time interval in data.table
  • R Leaflet Legend: specify order instead of alphabetical
  • Partial specialization of a class template in derived class affects base class
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • For loop with if condition on multiple R functions
  • Android changing fragment order inside FragmentPagerAdapter
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • wxPython: displaying multiple widgets in same frame
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • How to access EntityManager inside Entity class in EJB3
  • R - Combining Columns to String Based on Logical Match
  • Repeat a vertical line on every page in Report Builder / SSRS
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF