8  常用函数

8.1 常数

month.abb
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
month.name
 [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" 
letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

8.2 基础

c(1,2,3,4) # 拼接
[1] 1 2 3 4
seq(0,1,by=0.25) #序列
[1] 0.00 0.25 0.50 0.75 1.00
length(1:10)
[1] 10
rep(1:2,each=5)
 [1] 1 1 1 1 1 2 2 2 2 2
rep(1:2,time=5)
 [1] 1 2 1 2 1 2 1 2 1 2
head(1:10)
[1] 1 2 3 4 5 6
tail(1:10)
[1]  5  6  7  8  9 10
is.na(c(1:10,NA))
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
class(mtcars)
[1] "data.frame"
dim(mtcars)
[1] 32 11
names(mtcars)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
paste(2011,1:5,01,sep='-')
[1] "2011-1-1" "2011-2-1" "2011-3-1" "2011-4-1" "2011-5-1"
str(mtcars)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars)
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
 1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
 Median :19.20   Median :6.000   Median :196.3   Median :123.0  
 Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
 3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
 Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
 Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
 Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
 3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
 Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
       am              gear            carb      
 Min.   :0.0000   Min.   :3.000   Min.   :1.000  
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
 Median :0.0000   Median :4.000   Median :2.000  
 Mean   :0.4062   Mean   :3.688   Mean   :2.812  
 3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
 Max.   :1.0000   Max.   :5.000   Max.   :8.000  

8.3 统计


#统计
x=rnorm(1000) #随机数
head(x)
[1] -1.20005554  0.39673153 -0.03092921  0.90827612  0.70780874  1.17401448
min(x)
[1] -3.528692
max(x)
[1] 3.0422
range(x)
[1] -3.528692  3.042200
mean(x)
[1] 0.01667074
median(x)
[1] 0.04793839
sd(x)
[1] 1.008562
var(x)
[1] 1.017197
quantile(x)
         0%         25%         50%         75%        100% 
-3.52869250 -0.60665337  0.04793839  0.70892757  3.04219967 
fivenum(x)
[1] -3.52869250 -0.60688274  0.04793839  0.70893170  3.04219967
table(mtcars$cyl)

 4  6  8 
11  7 14 
table(mtcars$cyl,mtcars$cyl)
   
     4  6  8
  4 11  0  0
  6  0  7  0
  8  0  0 14