R repeating row selection

Ok, I'm pretty new to R, and I tried to find documentation on what I needed to do, but here is the problem.

I have a data.frame called heeds.data in the following form (some columns are omitted for simplicity) eval.num, eval.count, ... fitness, fitness.mean, green.h.0, green.v.0, offset.0, green.h.1, green.v.1, ... green. h.7, green.v.7, offset.7 ...

And I selected a line that meets the following criteria:

best.fitness <- min(heeds.data$fitness.mean[heeds.data$eval.count >= 10])
best.row <- heeds.data[heeds.data$fitness.mean == best.fitness]

Now I want all other rows to have green.h.0 and offset.7 columns (adjacent column section) equal to best.row

I thought it might work

heeds.best <- heeds.data$fitness[
  heeds.data$green.h.0 == best.row$green.h.0 & ...
]

But with 24 columns, this seems like a dumb method. Looking for something simpler with less manual tuning.

Here is a brief sample of data to show what I want

eval.num, eval.count, fitness, fitness.mean, green.h.0, green.v.0, offset.0
1         1           1500     1500          100        120        40
2         2           1000     1250          100        120        40
3         3           1250     1250          100        120        40
4         4           1000     1187.5        100        120        40
5         1           2000     2000          200        100        40
6         1           3000     3000          150        90         10
7         1           2000     2000          90         90         100
8         2           1800     1900          90         90         100

"" 4

eval.num, eval.count, fitness, fitness.mean, green.h.0, green.v.0, offset.0
1         1           1500     1500          100        120        40
2         2           1000     1250          100        120        40
3         3           1250     1250          100        120        40
4         4           1000     1187.5        100        120        40

, ,

!

+3
1

. , . DF:

-, ( which.min()):

R> bind <- which.min(DF[,"fitness.mean"])  # index of best row

apply() ( , , 5-7).

cmpfun r ( bind) all() , . [ drop=FALSE , , as.numeric() . ]

R> cmpfun <- function(r) all(r == DF[bind,5:7,drop=FALSE])  # compare to row bind

apply :

R> brows <- apply(DF[,5:7], 1, cmpfun)

, :

R> DF[brows, ]
  eval.num eval.count fitness fitness.mean green.h.0 green.v.0 offset.0
1        1          1    1500         1500       100       120       40
2        2          2    1000         1250       100       120       40
3        3          3    1250         1250       100       120       40
4        4          4    1000         1188       100       120       40
R> 

, - , , - , ( 5:7) .

+4

Source: https://habr.com/ru/post/1739706/


All Articles