selectRows: Select rows from a data frame according to reference IDs

View source: R/selectRows.R

selectRowsR Documentation

Select rows from a data frame according to reference IDs

Description

Select rows from a data frame according to reference IDs

Usage

selectRows(x, ref, by = NULL)

Arguments

x

a data frame to select

ref

reference IDs, by which the data frame x is to be filtered.

by

the column name (or NULL meaning "row names"), by which the selection is operated on.

Value

a data frame filtered and ordered by ref on by column. The result of thi function is similar to subset function. The difference is that subset function maintains the row orders in the data frame x, but selectRows maintains the order or ref. selectRows does not work if there are duplicates in both "ref" and "by" pointed column.

Examples

{
## Not run: 
### Unduplicated "by"
x = data.frame(V1 = letters[1:15], b = rnorm(15))
# Unduplicated "ref"
selectRows(x, ref = c(19, "a", "d", "c", "b"), by = "V1")

# Duplicated "ref"
selectRows(x, ref = c(19, "a", "a", "c", "b"), by = "V1")

ref1 = sample(1:20, 8)
print(ref1)
selectRows(x, ref = ref1)

### Duplicated "by"
x = data.frame(V1 = rep(letters[1:5], 1:5), b = rnorm(15))
# Unduplicated "ref"
# debugonce(selectRows)
selectRows(x, ref = c(19, "a", "d", "c", "b"), by = "V1")

# Duplicated "ref" (ERROR!)
selectRows(x, ref = c(19, "a", "a", "c", "b"), by = "V1")

## End(Not run)
}

paodan/funcTools documentation built on April 1, 2024, 12:01 a.m.