Tutorial by Examples: c

The %in% operator compares a vector with a set. v = "A" w = c("A", "A") w %in% v # TRUE TRUE v %in% w # TRUE Each element on the left is treated individually and tested for membership in the set associated with the vector on the right (consisting of all its...
To find every vector of the form (x, y) where x is drawn from vector X and y from Y, we use expand.grid: X = c(1, 1, 2) Y = c(4, 5) expand.grid(X, Y) # Var1 Var2 # 1 1 4 # 2 1 4 # 3 2 4 # 4 1 5 # 5 1 5 # 6 2 5 The result is a data.frame with one...
unique drops duplicates so that each element in the result is unique (only appears once): x = c(2, 1, 1, 2, 1) unique(x) # 2 1 Values are returned in the order they first appeared. duplicated tags each duplicated element: duplicated(x) # FALSE FALSE TRUE TRUE TRUE anyDuplicated(x) >...
To count how many elements of two sets overlap, one could write a custom function: xtab_set <- function(A, B){ both <- union(A, B) inA <- both %in% A inB <- both %in% B return(table(inA, inB)) } A = 1:20 B = 10:30 xtab_set(A, B) # inB ...
Imagine you have the following HTML: <div> <label>Name:</label> John Smith </div> And you need to locate the text "John Smith" after the label element. In this case, you can locate the label element by text and then use .next_sibling property: from ...
BeautifulSoup has a limited support for CSS selectors, but covers most commonly used ones. Use select() method to find multiple elements and select_one() to find a single element. Basic example: from bs4 import BeautifulSoup data = """ <ul> <li class="item&quo...
You can restrict the valid types used in a generic class by bounding that type in the class definition. Given the following simple type hierarchy: public abstract class Animal { public abstract String getSound(); } public class Cat extends Animal { public String getSound() { ...
Sometimes it's necessary to be able to extract values from an object using only references (ie. without transferring ownership). struct Token { pub id: u32 } struct User { pub token: Option<Token> } fn main() { // Create a user with an arbitrary token let user = User...
To setup Emacs for working with Clojure, install clojure-mode and cider package from melpa: M-x package-install [RET] clojure-mode [RET] M-x package-install [RET] cider [RET] Now when you open a .clj file, run M-x cider-jack-in to connect to a REPL. Alternatively, you can use C-u M-x (cider-jac...
All built-in Clojure collections are immutable and heterogeneous, have literal syntax, and support the conj, count, and seq functions. conj returns a new collection that is equivalent to an existing collection with an item "added", in either "constant" or logarithmic time. Wha...
A sequence is very much like a list: it is an immutable object that can give you its first element or the rest of its elements in constant time. You can also construct a new sequence from an existing sequence and an item to stick at the beginning. You can test whether something is a sequence using ...
A vector is denoted by square brackets: [] ;;=> [] [:foo] ;;=> [:foo] [:foo :bar] ;;=> [:foo :bar] [1 (+ 1 1) 3] ;;=> [1 2 3] In addition using to the literal syntax, you can also use the vector function to construct a vector: (vector) ;;=> [] (vector :foo) ;;=&...
For if you have to fine-tune what is published. import { Mongo } from 'meteor/mongo'; import { Meteor } from 'meteor/meteor'; import { Random } from 'meteor/random'; if (Meteor.isClient) { // established this collection on the client only. // a name is required (first parameter) and this...
On the server, you can create a publication like this. this.userId is the id of the user who is currently logged in. If no user is logged in, you might want to throw an error and respond to it. import Secrets from '/imports/collections/Secrets'; Meteor.publish('protected_data', function () { ...
Swift 3 let stackView = UIStackView() stackView.axis = .horizontal stackView.alignment = .fill // .leading .firstBaseline .center .trailing .lastBaseline stackView.distribution = .fill // .fillEqually .fillProportionally .equalSpacing .equalCentering let label = UILabel() label.text = "...
Swift let stackView = UIStackView() stackView.axis = .Vertical stackView.alignment = .Fill // .Leading .FirstBaseline .Center .Trailing .LastBaseline stackView.distribution = .Fill // .FillEqually .FillProportionally .EqualSpacing .EqualCentering let label = UILabel(frame: CGRectZero) label....
Using the dateutil library as in the previous example on parsing timezone-aware timestamps, it is also possible to parse timestamps with a specified "short" time zone name. For dates formatted with short time zone names or abbreviations, which are generally ambiguous (e.g. CST, which coul...
By default all datetime objects are naive. To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time. Fixed Offset Time Zones For time zones that are a fixed offset from UTC, in Python 3.2+, the datetime mod...
It is often helpful to have MATLAB print the 1st line of a function, as this usually contains the function signature, including inputs and outputs: dbtype <functionName> 1 Example: >> dbtype fit 1 1 function [fitobj,goodness,output,warnstr,errstr,convmsg] = fit(xdatain,ydatain,f...
You can commit changes made to specific files and skip staging them using git add: git commit file1.c file2.h Or you can first stage the files: git add file1.c file2.h and commit them later: git commit

Page 105 of 826