1. 程式人生 > >R中的一些基礎1106

R中的一些基礎1106

默認 and random ber ecif first https logical sqrt

1.R中NA,NaN,Inf代表什麽?

NA:缺失數據

NaN:無意義的數,比如sqrt(-2)

Inf:正無窮大

-Inf:負無窮大

2.確定一個數值型vector的第一個最值(最大/最小)的下標:

which.min(x)
which.max(x)


x    
numeric (logical, integer or double) vector or an R object for which the internal coercion to double works whose min or max is searched for.

3.對應的,確定一個矩陣每一行最值的下標:

max.col(m, ties.method = c("random", "first", "last"))
m    
numerical matrix

ties.method    
a character string specifying how ties are handled, "random" by default;

通常第一個參數默認是random,如果是first那麽就返回相同最大值中第一列,last與此相反。

4.判斷一個邏輯向量中,值為TRUE的下標:

which(x, arr.ind = FALSE, useNames = TRUE)
arrayInd(ind, .dim, .dimnames 
= NULL, useNames = FALSE)
which(LETTERS == "R")
which(ll <- c(TRUE, FALSE, TRUE, NA, FALSE, FALSE, TRUE)) #> 1 3 7

轉自:https://stat.ethz.ch/R-manual/R-devel/library/base/html/which.html

5.返回輸入值中的最大值和最小值

轉自:https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html

max(..., na.rm = FALSE)
min(..., na.rm 
= FALSE) pmax(..., na.rm = FALSE) pmin(..., na.rm = FALSE) pmax.int(..., na.rm = FALSE) pmin.int(..., na.rm = FALSE) ... numeric or character arguments (see Note). na.rm a logical indicating whether missing values should be removed.

用法:
min(5:1, pi) #-> one number
pmin(5:1, pi) #->  5  numbers

R中的一些基礎1106