在新列中查找行的最高和第二高值以及保存列名称

时间:2018-02-22 16:23:38

标签: r

我有一个问题。需要从表中查找前2个最大值并在新列中打印其名称。以下是数据集

ID  Fail1   Fail2   Fail3   Fail4
43324   10  5   4   9
42059   12  7   6   11
43321   14  9   8   13
43414   16  11  10  15
41517   18  13  12  17
43711   20  15  14  19
55675   22  17  16  21
55769   24  19  18  23
55631   26  21  20  25

现在对于每个ID,需要在同一个表中添加的新列中连接的第一个和第二个最大失败原因。 Data set Sample

2 个答案:

答案 0 :(得分:1)

这是一种从宽格式转换为长格式的方法,为每个ID选取两个最大值,并将相应的列名称作为新列附加到原始data.frame(使用join):

library(data.table)
DF[melt(DF, id.var = "ID")[order(-value), .(top = toString(variable[1:2])), by = ID], 
   on = "ID"]
      ID Fail1 Fail2 Fail3 Fail4          top
1: 55631    26    21    20    25 Fail1, Fail4
2: 55769    24    19    18    23 Fail1, Fail4
3: 55675    22    17    16    21 Fail1, Fail4
4: 43711    20    15    14    19 Fail1, Fail4
5: 41517    18    13    12    17 Fail1, Fail4
6: 43414    16    11    10    15 Fail1, Fail4
7: 43321    14     9     8    13 Fail1, Fail4
8: 42059    12     7     6    11 Fail1, Fail4
9: 43324    10     5     4     9 Fail1, Fail4

数据

library(data.table)
DF <- fread(
  "ID  Fail1   Fail2   Fail3   Fail4
43324   10  5   4   9
42059   12  7   6   11
43321   14  9   8   13
43414   16  11  10  15
41517   18  13  12  17
43711   20  15  14  19
55675   22  17  16  21
55769   24  19  18  23
55631   26  21  20  25"
)

答案 1 :(得分:0)

这样的事情,假设你的数据框叫public class ClipBoardFxApp extends Application { @Override public void start( final Stage primaryStage ) { final BorderPane root = new BorderPane(); final ImageView view = new ImageView(); final Button awtButton = new Button( "AWT" ); awtButton.setOnAction( event -> loadImageFromAwtClipboard( view ) ); final Button javaFXButton = new Button( "JavaFX" ); javaFXButton.setOnAction( event -> loadImageFromJavaFXClipboard( view ) ); root.setCenter( view ); final BorderPane buttonPane = new BorderPane(); buttonPane.setLeft( awtButton ); buttonPane.setRight( javaFXButton ); root.setBottom( buttonPane ); final Scene scene = new Scene( root, 400, 400 ); primaryStage.setScene( scene ); primaryStage.show(); } private void loadImageFromJavaFXClipboard( final ImageView view ) { System.out.println( "FX-Clipboard: Try to add Image from Clipboard..." ); final Clipboard clipboard = Clipboard.getSystemClipboard(); if ( clipboard.hasImage() ) { final Image image = clipboard.getImage(); view.setImage( image ); } else { new Alert( AlertType.INFORMATION, "No Image detected im Clipboard!" ).show(); } } private void loadImageFromAwtClipboard( final ImageView view ) { System.out.println( "AWT-Clipboard: Try to add Image from Clipboard..." ); try { final Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents( null ); if ( t != null && t.isDataFlavorSupported( DataFlavor.imageFlavor ) ) { final java.awt.image.BufferedImage img = (java.awt.image.BufferedImage) t.getTransferData( DataFlavor.imageFlavor ); final Image image = SwingFXUtils.toFXImage( img, null ); view.setImage( image ); } else { new Alert( AlertType.INFORMATION, "No Image detected im Clipboard!" ).show(); } } catch ( final UnsupportedFlavorException | IOException e ) { e.printStackTrace(); } } public static void main( final String[] args ) { launch( args ); } }

dat

如果首先存在并列关系,则会给出所有首先匹配的列(即使有两个以上),也没有列为第二列。如果第二个并列第一个并且没有第一个并列,那么这将使列具有最高值,所有列将为第二个。

这是@ Uwe的答案的dat$top2 = apply(dat[ , grepl("Fail", names(dat))], 1, function(r) { paste(names(r)[which(rank(-r, ties.method="first") %in% c(1:2))], collapse=", ") }) 版本:

tidyverse