cranvas (version 0.8.5)

link_knn: k-Nearest neighbor linking

Description

When a number of elements in a data are brushed, their k-nearest neighbors (based on a certain distance measure) are brushed as well.

Usage

link_knn(mf1, var1 = NULL, mf2 = NULL, var2 = var1, k = 10)

Arguments

var1
the variable names or column indices of the first mutaframe to be used to calculate distances
var2
(optional) variable names or column indices of the second mutaframe (by default the same as var1)
k
the number of nearest neighbors to select
mf1
the first mutaframe
mf2
(optional) the second mutaframe; default NULL means mf1 will be linked to itself

Value

Similar to categorical linking (link_cat), this function also links two mutaframes together (or one mutaframe to itself), and id's of listeners are returned.

Details

A center point for the variables based on the selected rows is calculated in the first dataset, then the k nearest rows in the second dataset (if not provided, it will be the same as the first dataset) to this center are selected. Only the Euclidean distance has been implemented at the moment.

Examples

Run this code

### (1) linking to oneself
data(flea, package = "tourr")
qflea <- qdata(flea, color = species)
qscatter(tars1, tars2, data = qflea)

## brush the nearest 10 points around the center
id <- link_knn(qflea, c("tars1", "tars2"), k = 10)

## remove the linking
remove_link(qflea, id)


### (2) variables on the same scale
mf <- qdata(data.frame(x = rnorm(300), y = rnorm(300)), size = 3)
qscatter(x, y, data = mf)

id <- link_knn(mf, c("x", "y"), k = 20)

remove_link(mf, id)


### (3) link two datasets

mf1 <- qdata(flea, color = species)
mf2 <- qdata(subset(flea, species == "Concinna "))

qscatter(tars1, tars2, data = mf1)
qscatter(tars1, tars2, data = mf2)

id <- link_knn(mf1, c("tars1", "tars2"), mf2, c("tars1", "tars2"))

remove_link(mf1, id[1])
remove_link(mf2, id[2])

cranvas_off()

Run the code above in your browser using DataCamp Workspace