Learn R Programming

expss (version 0.5.5)

match_row: Match finds value in rows or columns/index returns value by index from rows or columns

Description

match finds value in rows or columns. index returns value by index from row or column. One can use functions as criteria for match. In this case position of first value on which function equals to TRUE will be returned. For convenience there are special predefined functions - see criteria. If value doesn't found NA will be returned.

Usage

match_row(criterion, ...)
match_col(criterion, ...)
index_row(index, ...)
index_col(index, ...)

Arguments

criterion
Vector of values to be matched, or function.
...
data. Vectors, matrixes, data.frames, lists. Shorter arguments will be recycled.
index
vector of positions in rows/columns from which values should be returned.

Value

vector with length equals to number of rows for *_row and equals to number of columns for *_col.

Examples

Run this code
# toy data
v1 = 1:3
v2 = 2:4
v3 = 7:5

# postions of 1,3,5 in rows
match_row(c(1, 3, 5), v1, v2, v3) # 1:3
# postions of 1,3,5 in columnss
match_col(1, v1, v2, v3) # c(v1 = 1, v2 = NA, v3 = NA)

# postion of first value greater than 2
ix = match_row(gt(2), v1, v2, v3) 
ix # c(3,2,1)
# return values by result of previous 
index_row(ix, v1, v2, v3) # c(7,3,3)

# the same actions with data.frame
dfs = data.frame(v1, v2, v3)

# postions of 1,3,5 in rows
match_row(c(1, 3, 5), dfs) # 1:3
# postions of 1,3,5 in columnss
match_col(1, dfs) # c(v1 = 1, v2 = NA, v3 = NA)

# postion of first value greater than 2
ix = match_row(gt(2), dfs) 
ix # c(3,2,1)
# return values by result of previous 
index_row(ix, dfs) # c(7,3,3)

Run the code above in your browser using DataLab