[Solved] Could you please help me to understand an R code?


First of all, in general,

var <- expr

evaluates the R expression expr and assigns the result to the variable var. If the statement occurs inside a function, then var becomes a function-local variable, otherwise, it becomes a global variable.

c(0,0,0,1,0,1,0,1,1,1,1,0)

Combines 12 double literals into a double vector in the order given.

matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, byrow=T )

Creates a matrix from the vector with 4 rows and 3 columns, populating the matrix one row at a time from top to bottom (left-to-right within each row).

nrow(patterns)

Returns the number of rows in the patterns matrix.

sample(nrow(patterns))

Returns an integer vector of nrow(patterns) elements, by basically randomly scrambling the set of integers from 1 to nrow(patterns).

patterns[sample(nrow(patterns)),]

Indexes the patterns matrix. This type of indexing basically allows you to extract a “submatrix” of the original matrix. The argument to the left of the comma specifies the rows to select, and to the right specifies the columns to select. An omitted argument is equivalent to specifying all indexes of that dimension. This particular expression selects all rows and all columns in the matrix, but scrambles the row order.

t(ps)

Transposes the matrix ps.

as.vector(t(ps))

Flattens the transposed matrix into a vector. Note that this is by column, which is the opposite of how the matrix was originally constructed from a vector earlier. Note that, because this is the last statement in the enclosing function, it will automatically become the return value of the function when it is executed.

function(i) {
    ps <- patterns[sample(nrow(patterns)),]
    as.vector(t(ps))
}

Defines a function taking one parameter i and that executes the two statements I explained above. Note that patterns is effectively closured by this function.

1:100

Creates an integer vector consisting of 100 elements, the integers from 1 to 100.

lapply(1:100, function(i) { ... } )

Executes the function given in the second argument once for each element of the first argument, passing the element as the first argument of the function when it is called for that particular element. In this case, the result is the function will be executed 100 times, passing integers 1 to 100 as the argument to the i parameter for each respective invocation. lapply() always returns the return value of every single execution of the function, combined into a list object.

unlist( ... )

Converts the list into a single homogenous (non-list) object. How this works depends on the exact nature of its argument, but in this case, it will combine the vectors returned by every function invocation into a single vector.

input[-1]

Returns the entire vector input excluding its first element.

input[1]

Returns the first element of the vector input.

c(input[-1],input[1])

Combines the two previous values. The end result is the first element has been moved to the end of the vector.

cbind(input, teach)

Performs a “column-bind” of the two aforementioned vectors. This means each vector will be treated as an length(vector)-by-1 matrix, and the two matrices will be combined into a length(vector)-by-2 matrix. (If the lengths weren’t equal, the function would still succeed, but it would recycle any short vector inputs and print a warning message.)

1

solved Could you please help me to understand an R code?