Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of braces {...} in j

Tags:

r

data.table

I have been studying a datacamp course called "Data Analysis in R, the data.table Way".

The instruction of the exercise is as follows,

  • Select the b group without using ==.
  • Select the b and c groups.
  • Select the first row of the b and c groups using mult.
  • Use by=.EACHI and .SD to select the first and last row of the b and c groups.
  • Extend the previous command to print out the group before returning the first and last row from it.

The layout is as follows,

# This is your data.table `DT`. The keys are set to `A` and `B`
DT <- data.table(A = letters[c(2,1,2,3,1,2,3)], B = c(5,4,1,9,8,8,6), C = 6:12)
setkey(DT, A, B)
# Select the `b` group
# `b` and `c` groups
# The first row of the `b` and `c` group
# `by=.EACHI` and `.SD` 
# Print out all the data in the two groups before you return
# the first and last row of each group again. Use {} and .N

I do understand how the first 4 to be done but then when I proceed to the last instruction I got stuck. I do not understand how {} can be used and eventually I found out that the solution for that is:

DT[c("b", "c"), {print(.SD); .SD[c(1, .N)]}, by = .EACHI]

I do not understand how the syntax in j works, may anyone explain it to me? Thanks.

like image 517
user66794 Avatar asked Aug 27 '15 07:08

user66794


People also ask

What are braces in maths?

Braces are extensively used in mathematics to denote numerical sets of numbers. Odd numbers {1, 3, 5, 7, 9….} Note that any set of numbers that have a specific property or have any characteristic in common is enclosed inside braces. Another usage of braces in English language is to indicate choices or preferences.

How to use braces in English language?

Another usage of braces in English language is to indicate choices or preferences. Choose your material {cloth, thread, needle, scissors} and move to step B. Select one item {tea, soda, coffee, wine} and wait for your turn. Note that the above examples show equal options or choices which are enclosed inside braces.

Which type of braces are best for You?

Metal braces are made of strong, lightweight materials, including titanium and alloy. They do not rust or set off metal detectors. 2 Metal braces are the most common type of braces, and they are often the least expensive option. One downside is that they are the most noticeable on your teeth.

What numbers do you put inside braces?

Odd numbers {1, 3, 5, 7, 9….} Note that any set of numbers that have a specific property or have any characteristic in common is enclosed inside braces. Another usage of braces in English language is to indicate choices or preferences. Choose your material {cloth, thread, needle, scissors} and move to step B.


1 Answers

As you know from reading help("data.table"), j expects

A single column name, single expresson of column names, list() of expressions of column names, an expression or function call that evaluates to list (including data.frame and data.table which are lists, too), or (when with=FALSE) a vector of names or positions to select.

You should also read help("{"). { can combine expressions. It returns the last evaluated expression. Thus, as far as data.table is concerned it only gets handed the last expression, i.e., .SD[c(1, .N)], and it knows what to do with that (e.g., combine the results for each group). data.table doesn't need to know what to do with print(.SD), which however is evaluated within the data.table's frame.

like image 144
Roland Avatar answered Oct 02 '22 07:10

Roland