Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Tuesday, December 21, 2010

Set Operations in R

R can perform different operations in sets, such as union, intersection, asymmetric difference of two sets, etc. Specifically, the following operations are available in R for set operations.

Operator
Usage
Definition
union
union(x, y)
Union of sets x and y
intersect
intersect(x, y)
Intersection of sets x and y
setdiff
setdiff(x, y)
Asymmetric difference between sets x and y (Elements in x but not in y)
setequal
setequal(x, y)
If sets x and y have the same elements
is.element
is.element(el, set)
If el is an element of set

Examples:

> x <- c(sort(sample(1:20, 9)),NA)
> y <- c(sort(sample(3:23, 7)),NA)
> x
[1]  1  3  5  8 11 17 18 19 20 NA
> y
[1]  7 11 15 16 17 19 22 NA
> union(x, y)
[1]  1  3  5  8 11 17 18 19 20 NA  7 15 16 22
> intersect(x, y)
[1] 11 17 19 NA
> setdiff(x, y)
[1]  1  3  5  8 18 20
> setdiff(y, x)
[1]  7 15 16 22
> setequal(x, y)
[1] FALSE

Note that each of union, intersect, setdiff and setequal will discard any duplicated values in the arguments. Look at the following example:

> x
[1]  1  3  5  8 11 17 18 19 20 NA
> x2 <- c(x, 1, 3, 5, 8)
> x2
[1]  1  3  5  8 11 17 18 19 20 NA  1  3  5  8
> setdiff(x, y)
[1]  1  3  5  8 18 20
> setdiff(x2, y)
[1]  1  3  5  8 18 20
> setequal(x, x2)
[1] TRUE

Although x and x2 have different length, they have the same UNIQUE elements so setequal(x, x2) returns a TRUE value.

is.element(x, y) is identical to x %in% y which is already discussed here. The return value of is.element is a vector of TRUE and FALSE with the same length as x, which indicates whether each element of x is an element of y or not.

> is.element(x, y)  # vector of length 10
[1] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
> is.element(y, x)  # vector of length 8
[1] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE

Wednesday, December 1, 2010

Complex numbers in R

We sometimes encounter the situations of using complex numbers in our computation. For example, the square root of -1 can be denoted as 1*i. Complex numbers are implemented in the "base" package, it’s very easy to work with them. To construct a complex number x + iy, you use complex and specify its real and imaginary components explicitly as follows:

> x <- 2
> y <- 3
> z1 <- complex(real = x, imaginary = y)
> z1
[1] 2+3i

You can convert other objects to class "complex" using as.complex and test if an object is complex with is.comple

> z2 <- as.complex(-5)
> z2
[1] -5+0i
> is.complex(z2)
[1] TRUE

There are five basic mathematical operations that works on complex numbers, Re, Im, Mod, Arg, and Conj. First, you may want to extract the real and imaginary components of a complex number. You can do this using Re and Im, respectively. You can also find the modulus and complex argument of a complex number with Mod and Arg. Finally, you can take the complex conjugate of a complex number with the help of Conj.

> z3 <- complex(real = 1.3, imaginary = 6) 
> z3
[1] 1.3+6i
> Re(z3)
[1] 1.3
> Im(z3)
[1] 6
> Mod(z3)
[1] 6.139218
> Arg(z3)
[1] 1.357428
> Conj(z3)
[1] 1.3-6i

Monday, November 29, 2010

Find all the matches between two vectors using %in% in R

Suppose you want to know all of the matches between one character vector and another, you can do that with the help of which and %in% in R. For example,

> allclasses <- c("physics", "chemistry", "statistics", "mathematics", "biology", "history", "english")
> registered <- c("physics", "mathematics", "history")
> which(allclasses %in% registered)
[1] 1 4 6

This also works with numeric vectors. For example, a numeric set B is a subset of A, and you want to select all those elements that are included in A but not B. You can do the following:

> A <- c(1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
> B <- c(1, 5, 21, 89)
> A[!(A %in% B)]
[1]  2  3  8 13 34 55

Saturday, November 27, 2010

Value of last evaluated expression in R

R saves the value of last evaluated expression in a variable called .Last.value. You can directly use this variable instead of running the last expression again.

For example:

> x <- 1:10
> x^2
[1] 1 4 9 16 25 36 49 64 81 100
> y <- .Last.value
> y
[1] 1 4 9 16 25 36 49 64 81 100