## How to select Nth element in subgroup using data.table?

Simple task: I need to extract (lets say) 1st and 2nd elements of subgroup.

DT <- data.table (X=c(1,2), Y=round(rnorm(5),1), key="X"); DT X Y 1: 1 -0.4 2: 1 0.5 3: 1 1.2 4: 2 1.0 5: 2 -0.3

So, I need to get DT2, which would be

1: 1 -0.4 2: 1 0.5 3: 2 1.0 4: 2 -0.3

This does not work:

DT2 <- DT[, .(first = .SD[1], second=.SD[2]), by=X]

What works?

This is how I would do it and I think the *intended* way (by the package authors):

DT[, .SD[1:2], by = X]

**[PDF] Cheat sheet DataTable R.indd,** DT <- data.table(V1=c(1L,2L), and call it DT. V2=LETTERS[1:3],. V3=round(rnorm (4),4),. V4=1:12). Subsetting Rows Using i. > DT[3:5,]. Select 3rd to 5th row. For nth_value(), a single integer specifying the position. Negative integers index from the end (i.e. -1L will return the last value in the vector). If a double is supplied, it will be silently truncated. order_by: An optional vector used to determine the order. default: A default value to use if the position does not exist in the input.

We can do a group by `head`

DT[, head(.SD, 2), X]

Or extract the row index (`.I`

) and subset

DT[DT[, .I[1:2], X]$V1]

**nth: Extract the first, last or nth value from a vector in dplyr,** nth: Extract the first, last or nth value from a vector. In dplyr: A Grammar of Data Manipulation For nth_value() , a single integer specifying the position. Negative� The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent. n can be a number, a keyword, or a formula. Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of a particular type, of its parent.

Following code also produces the same result. If ordering is important, setkey or order can be run beforehand.

DT <- data.table (X=c(1,2), Y=round(rnorm(5),1), key="X"); DT X Y 1: 1 0.5 2: 1 1.4 3: 1 0.0 4: 2 -1.5 5: 2 -0.3 DT[,Y[1:2],.(X)] X V1 1: 1 0.5 2: 1 1.4 3: 2 -1.5 4: 2 -0.3

**Keys and fast binary search based subset,** first introduce the concept of key in data.table, and set and use keys to In the “ Introduction to data.table” vignette, we saw how to subset rows in i using logical But, a person (for example) has at least two names - a first and a second So we simply select the column arr_delay for those row indices in the� There is a little-known filter that can be added to :nth-child according to the CSS Selectors specification: The ability to select the :nth-child of a subset of elements, using the of format. Suppose you have a list of mixed content: Some have the class .video , some have the class .picture , and you want to select the first 3 pictures.

Here's a solution using tidyverse/dplyr. The first section uses tibble, which is an extension of the usual data frame.

library(tidyverse) df <- tibble( x = c(1, 1, 1, 2, 2), y = c(-0.4, 0.5, 1.2, 1.0, -0.3) ) df # A tibble: 5 x 2 x y <dbl> <dbl> 1 1 -0.4 2 1 0.5 3 1 1.2 4 2 1 5 2 -0.3 df2 <- df %>% group_by(x) %>% slice(1:2) %>% ungroup() df2 # A tibble: 4 x 2 x y <dbl> <dbl> 1 1 -0.4 2 1 0.5 3 2 1 4 2 -0.3

**Advanced tips and tricks with data.table – andrew brooks,** Some more advanced functionality from data.table creator Matt Dowle here. Extract second element of each list in gearL1 and create row gearL1 . I was looking for – applying a function to a subset of columns with . Further, since this is just selecting one item (the 2nd) from each list cell, you probably� In this article, we will learn how to retrieve the Nth value in a Row, Column, and range. We use a combination of “OFFSET”, “Index”, and “ROW” functions in Microsoft Excel 2010. OFFSET: - One of the rows and columns in a range that is a specified number of the cells or range of cells has a reference returned references.

**Everything I know about R subsetting,** The second is also pretty easy: if you subset with a character vector, you If you want to select a single element, you can subset with double To get the first two rows and the first four columns of this data frame, we can do: The :nth-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent. n can be a number, a keyword, or a formula. Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.

**4 Subsetting,** Use [ to select any number of elements from a vector. This means that you can use a 2 column matrix to subset a matrix, a 3 column matrix to 3 3 1 c # There are two ways to select columns from a data frame # Like a list df[c("x", In the second, mtcars becomes a list because you are changing the object it is bound to . I have tried using the ReportKey as follow : SELECT MyRowName From TAB_MySubFormName WHERE TAB_MySubFormName.ReportKey=" & ReportKey but the result of the ReportKey is always 1 which I guess it's the first element even thought I have clicked on the last element in the table

**R data.table symbols and operators you should know,** With that in mind, we'll look at some special ways to subset, count, and Keep in mind that i and j are similar to base R's bracket ordering: rows first, columns second. You can select data.table columns the typical base R way, with a I can't just look for "R;" because the last item in each string doesn't� This is in response to a question asked on the r-help mailing list.. Here are lots of examples of how to find top values by group using sql, so I imagine it's easy to convert that knowledge over using the R sqldf package.

##### Comments

`.SD`

stands for subset of data.table, set of columns. What you wanted to assign to`first`

and`second`

are single columns. Just use`Y[1L]`

and`Y[2L]`

instead of`.SD`

.