How to find highest value in a data frame?

前端 未结 3 729
自闭症患者
自闭症患者 2021-02-14 13:57

I have a dataframe x with this values:

   x1  x2  x3
1  NA   4   1
2  NA   3  NA
3   4  NA   2
4  NA   1  11
5  NA   2  NA
6   5  NA   1
7   5   9           


        
相关标签:
3条回答
  • 2021-02-14 14:12

    Use max() with the na.rm argument set to TRUE:

    dat <- read.table(text="
       x1  x2  x3
    1  NA   4   1
    2  NA   3  NA
    3   4  NA   2
    4  NA   1  11
    5  NA   2  NA
    6   5  NA   1
    7   5   9  NA
    8  NA   2  NA", header=TRUE)
    

    Get the maximum:

    max(dat, na.rm=TRUE)
    [1] 11
    
    0 讨论(0)
  • 2021-02-14 14:21

    To find the sum of a column, you might want to unlist it first;

    max(unlist(myDataFrame$myColumn), na.rm = TRUE)
    

    Source

    0 讨论(0)
  • 2021-02-14 14:24

    you could write a column maximum function, colMax.

    colMax <- function(data) sapply(data, max, na.rm = TRUE)
    

    Use colMax function on sample data:

    colMax(x)
    #    x1     x2     x3
    #   5.0    9.0    11.0   
    
    0 讨论(0)
提交回复
热议问题