As @max mentioned earlier you should use a reprex. If you can prune your data down to a small set then you can build a data.frame with the data in a reprex along with an example that shows the issues you are running into.
A prose description isn't sufficient, you also need to make a simple reprex that:
- Builds the input data you are using.
- The function you are trying to write, even if it doesn't work.
- Usage of the function you are trying to write, even if it doesn't work.
- Builds the output data you want the function to produce.
You can learn more about reprex's here:
Right now the is an issue with the version of reprex that is in CRAN so you should download it directly from github.
Until CRAN catches up with the latest version install reprex with
devtools::install_github("tidyverse/reprex")
The reason we ask for a reprex is that it is the easiest and quickest way for someone to understand the issue you are running into and answer it.
Nearly everyone here who is answering questions is doing it on their own time and really appreciate anything you can do to minimize that time.
For example here is a reprex of the sample data you provided earlier.
suppressPackageStartupMessages(library(tidyverse))
tbl <- tribble(
~Y, ~altitude, ~frp, ~avg_rain, ~pop_den, ~so_type, ~dist_road, ~dist_settle, ~dist_river,
2, 1136, 3, 525, 2.247755, 1, 2305, 5344.4789, 38439.70,
2, 1131, 3, 525, 2.247755, 1, 2306, 5400.0556, 38043.63,
13, 1129, 1, 525, 2.247755, 1, 1637, 3095.3570, 37323.73,
13, 1126, 1, 525, 2.247755, 1, 1637, 414.0876, 36834.82,
3, 1136, 0, 525, 2.247755, 1, 1637, 4989.1892, 40934.57,
13, 1131, 2, 525, 2.247755, 1, 1637, 2032.1340, 40266.40
)
tbl
#> # A tibble: 6 x 9
#> Y altitude frp avg_rain pop_den so_type dist_road dist_settle
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2. 1136. 3. 525. 2.25 1. 2305. 5344.
#> 2 2. 1131. 3. 525. 2.25 1. 2306. 5400.
#> 3 13. 1129. 1. 525. 2.25 1. 1637. 3095.
#> 4 13. 1126. 1. 525. 2.25 1. 1637. 414.
#> 5 3. 1136. 0. 525. 2.25 1. 1637. 4989.
#> 6 13. 1131. 2. 525. 2.25 1. 1637. 2032.
#> # ... with 1 more variable: dist_river <dbl>
Created on 2018-03-11 by the reprex package (v0.2.0).