我有一個數據幀x
這個值:如何在數據框中找到最高值?
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
一個簡單的問題:如何獲得最高的價值?(11)
我有一個數據幀x
這個值:如何在數據框中找到最高值?
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
一個簡單的問題:如何獲得最高的價值?(11)
使用max()
與na.rm
參數設置爲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)
獲取最大:
max(dat, na.rm=TRUE)
[1] 11
你可以寫專欄的最大功能,colMax
。
colMax <- function(data) sapply(data, max, na.rm = TRUE)
樣本數據
使用colMax功能:
colMax(x)
# x1 x2 x3
# 5.0 9.0 11.0