array, matrix, list, dataframe

array
  • array can be k dimensional
  • construct arrays by the array function:

Z <- array(data_vector, dim_vector)

For example, if the vector h contains 24 or fewer, numbers then the command

Z <- array(h, dim=c(3,4,2))

would use h to set up 3 by 4 by 2 array in Z.

If the size of h is exactly 24 the result is the same as

Z <- h ; dim(Z) <- c(3,4,2)

However if h is shorter than 24, its values are recycled from the beginning again to make it up to size 24.

As an extreme but common example

Z <- array(0, c(3,4,2))

makes Z an array of all zeros.

  • Indexing arrays by [ ]:
    Z[1,2,1]
    Z[ , , 2]

  • Arrays may be used in arithmetic expressions and the result is an array formed by element-by-element operations on the data vector. The dim attributes of operands generally need to be the same, and this becomes the dimension vector of the result.

  • Calculate outer product of two arrays:
    The outer product is formed by the special operator %o%:

ab <- a %o% b

An alternative is

ab <- outer(a, b, " * ")

The multiplication function can be replaced by an arbitrary function of two variables. For example if we wished to evaluate the function f(x; y) = cos(y)/(1 + x^2) over a regular grid of values with x- and y-coordinates defined by the R vectors x and y respectively, we could proceed as follows:

f <- function(x, y) cos(y)/(1 + x^2)
z <- outer(x, y, f)

Matrix
  • Matrix multiplication

The operator %*% is used for matrix multiplication. An n by 1 or 1 by n matrix may of course be used as an n-vector if in the context such is appropriate. Conversely, vectors which occur in matrix multiplication expressions are automatically promoted either to row or column vectors, whichever is multiplicatively coherent, if possible, (although this is not always unambiguously possible, as we see later).

If, for example, A and B are square matrices of the same size, then

A * B

is the matrix of element by element products and

A %*% B

is the matrix product. If x is a vector, then

x %% A %% x

is a quadratic form.

The function crossprod() forms “crossproducts”, meaning that crossprod(X, y) is the same as t(X) %*% y but the operation is more efficient. If the second argument to crossprod() is omitted it is taken to be the same as the first.

The meaning of diag() depends on its argument. diag(v), where v is a vector, gives a diagonal matrix with elements of the vector as the diagonal entries. On the other hand diag(M), where M is a matrix, gives the vector of main diagonal entries of M. This is the same convention as that used for diag() in MATLAB. Also, somewhat confusingly, if k is a single numeric value then diag(k) is the k by k identity matrix!

  • Forming partitioned matrices

cbind() and rbind()

As we have already seen informally, matrices can be built up from other vectors and matrices by the functions cbind() and rbind(). Roughly cbind() forms matrices by binding together matrices horizontally, or column-wise, and rbind() vertically, or row-wise.

Lists

An R list is an object consisting of an ordered collection of objects known as its components.
There is no particular need for the components to be of the same mode or type, and, for example, a list could consist of a numeric vector, a logical value, a matrix, a complex vector, a character array, a function, and so on. Here is a simple example of how to make a list:

Lst <- list(name=“Fred”, wife=“Mary”, no.children=3,
child.ages=c(4,7,9))

Data frame

Objects satisfying the restrictions placed on the columns (components) of a data frame may be used to form one using the function data.frame():

accountants <- data.frame(home=statef, loot=incomes, shot=incomef)

A list whose components conform to the restrictions of a data frame may be coerced into a data frame using the function as.data.frame()

The simplest way to construct a data frame from scratch is to use the read.table() function to read an entire data frame from an external file.

  • attach() and detach()
    The $ notation, such as accountants$home, for list components is not always very convenient. A useful facility would be somehow to make the components of a list or data frame temporarily visible as variables under their component name, without the need to quote the list name explicitly each time.
    The attach() function takes a ‘database’ such as a list or data frame as its argument. Thus suppose lentils is a data frame with three variables lentils$u, lentils$v, lentils$w. The attach

attach(lentils)

places the data frame in the search path at position 2, and provided there are no variables u, v or w in position 1, u, v and w are available as variables from the data frame in their own right. At this point an assignment such as

u <- v+w

does not replace the component u of the data frame, but rather masks it with another variable u in the working directory at position 1 on the search path. To make a permanent change to the data frame itself, the simplest way is to resort once again to the $ notation:

lentils$u <- v+w

However the new value of component u is not visible until the data frame is detached and attached again.

To detach a data frame, use the function

detach()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值