Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于dataframes R中排名列的数据帧子集列表_R_List_Dataframe_Subset_Ranking - Fatal编程技术网

基于dataframes R中排名列的数据帧子集列表

基于dataframes R中排名列的数据帧子集列表,r,list,dataframe,subset,ranking,R,List,Dataframe,Subset,Ranking,我有一个数据帧列表。我只想对包含分数比第二排名分数低10倍的行的数据帧进行子集,删除所有其他数据帧。你知道怎么做吗?谢谢 >Output $E1 ID model score E1 AAA 2 E1 BBB 100 E1 CCC 130 E1 ZZZ 120 E1 YYY 128 $E2 ID model score E2 XXX 130

我有一个数据帧列表。我只想对包含分数比第二排名分数低10倍的行的数据帧进行子集,删除所有其他数据帧。你知道怎么做吗?谢谢

>Output
$E1
  ID    model   score
  E1      AAA    2
  E1      BBB    100
  E1      CCC    130
  E1      ZZZ    120
  E1      YYY    128

$E2
  ID    model   score
  E2      XXX    130
  E2      ASD    144
  E2      DFE    142
  E2      FGS    145
  E2      GFH    124
首选结果:

>Output_subset
$E1
  ID    model   score
  E1      AAA    2
  E1      BBB    100
  E1      CCC    130
  E1      ZZZ    120
  E1      YYY    128

您可以编写一个函数来检查两个分数之间的条件:

check_data <- function(df) {
   x <- sort(df$score)
   x[1] < (x[2]/10)
}
或保持呼噜声:

资料

我们也可以使用R基地的sapply

数据
Filter(check_data, Output)

#$E1
#  ID model score
#1 E1   AAA     2
#2 E1   BBB   100
#3 E1   CCC   130
#4 E1   ZZZ   120
#5 E1   YYY   128
purrr::keep(Output, check_data)
Output <- list(E1 = structure(list(ID = c("E1", "E1", "E1", "E1", "E1"), 
model = c("AAA", "BBB", "CCC", "ZZZ", "YYY"), score = c(2L, 
100L, 130L, 120L, 128L)), class = "data.frame", row.names = c(NA, 
-5L)), E2 = structure(list(ID = c("E2", "E2", "E2", "E2", "E2"
), model = c("XXX", "ASD", "DFE", "FGS", "GFH"), score = c(130L, 
144L, 142L, 145L, 124L)), class = "data.frame", row.names = c(NA, -5L)))
Output[sapply(Output, function(x) 
      with(head(x[order(x$score), ], 2), score[1] < (score[2]/10)))]
Output <- list(E1 = structure(list(ID = c("E1", "E1", "E1", "E1", "E1"), 
model = c("AAA", "BBB", "CCC", "ZZZ", "YYY"), score = c(2L, 
100L, 130L, 120L, 128L)), class = "data.frame", row.names = c(NA, 
-5L)), E2 = structure(list(ID = c("E2", "E2", "E2", "E2", "E2"
), model = c("XXX", "ASD", "DFE", "FGS", "GFH"), score = c(130L, 
144L, 142L, 145L, 124L)), class = "data.frame", row.names = c(NA, -5L)))