Find Coordinates of Specific Values in a data.frame
Source:R/valueCoordinates.R
valueCoordinates.Rd
This function locates the row and column coordinates of values in a data.frame that satisfy a given condition. It's particularly useful for identifying the positions of specific or problematic values in large datasets.
Arguments
- df
A data.frame to be searched.
- value
The value to search for. Default is NA.
- eq_fun
A function used to check equality. It should take two arguments: the current value in the data.frame and the `value` parameter. Default is the internal `eq_fun` function that uses `identical()` for non-NA values and `is.na()` for NA.
Value
A data.frame with two columns:
- column
The column numbers where the specified condition was met
- row
The row numbers where the specified condition was met
The returned data.frame is sorted first by column, then by row.
Details
The function performs the following steps: 1. Creates a logical matrix where TRUE indicates values meeting the specified condition. 2. Finds the row and column indices of TRUE values. 3. Combines these indices into a data.frame. 4. Sorts the results by column, then by row.
If no custom equality function is provided, the function uses the internal `eq_fun` which checks for NA values with `is.na()` and uses `identical()` for all other values.
Examples
# Create a sample data.frame
df <- data.frame(
a = c(1, NA, 3),
b = c(NA, 2, NA),
c = c(3, 2, 1)
)
# Find coordinates of NA values
valueCoordinates(df)
#> column row
#> 2 1 1
#> 1 2 2
#> 3 2 3
# Find coordinates of the value 2
valueCoordinates(df, 2)
#> column row
#> 1 2 2
#> 2 3 2
# Find coordinates of values greater than 2
valueCoordinates(df, 2, function(x, y) x > y)
#> column row
#> 2 1 1
#> 1 3 3
# Find coordinates of values within a range
valueCoordinates(df, c(1, 3), function(x, y) x >= y[1] & x <= y[2])
#> column row
#> 1 1 1
#> 5 1 2
#> 3 2 2
#> 4 3 1
#> 2 3 3
#> 6 3 3