一些R命令(6)
程序员文章站
2024-03-05 10:57:48
...
>airquality
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 NA 194 8.6 69 5 10
11 7 NA 6.9 74 5 11
.....................................
.....................................
.....................................
> complete.cases(airquality)
[1] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE
[9] TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
[17] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[25] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
[33] FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
[41] TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
[49] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
[57] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
[65] FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
[73] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
[81] TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE
[89] TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
[97] FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE
[105] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
[113] TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE
[121] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[129] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[137] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[145] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE
[153] TRUE
> which(complete.cases(airquality)==F)
[1] 5 6 10 11 25 26 27 32 33 34 35 36
[13] 37 39 42 43 45 46 52 53 54 55 56 57
[25] 58 59 60 61 65 72 75 83 84 96 97 98
[37] 102 103 107 115 119 150
> sum(which(complete.cases(airquality)==F))
[1] 2463
> sum(complete.cases(airquality))
[1] 111
> na.omit(airquality)#删除缺失值的数据
> x=1:10
> x[12]=3
> x
[1] 1 2 3 4 5 6 7 8 9 10 NA 3
> x1=append(x,77,after=5)
> x1
[1] 1 2 3 4 5 77 6 7 8 9 10 NA 3
> cbind(1:5,rnorm(5))
[,1] [,2]
[1,] 1 -0.2571430
[2,] 2 1.8732397
[3,] 3 1.3009167
[4,] 4 -0.1391500
[5,] 5 -0.3810899
> rbind(1:5,rnorm(5))
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.000000 3.000000 4.0000000
[2,] -0.4939403 1.349063 -1.744914 0.3005233
[,5]
[1,] 5.0000000
[2,] 0.1660628
> cbind(1:3,4:6)
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> rbind(1:3,4:6)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
> (x=rbind(1:5,runif(5),runif(5),1:5,7:11))
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.0000000 3.0000000 4.0000000
[2,] 0.5647087 0.3511176 0.4897394 0.1811871
[3,] 0.5410171 0.0695839 0.3339522 0.7064547
[4,] 1.0000000 2.0000000 3.0000000 4.0000000
[5,] 7.0000000 8.0000000 9.0000000 10.0000000
[,5]
[1,] 5.0000000
[2,] 0.5039326
[3,] 0.2315622
[4,] 5.0000000
[5,] 11.0000000
> x
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.0000000 3.0000000 4.0000000
[2,] 0.5647087 0.3511176 0.4897394 0.1811871
[3,] 0.5410171 0.0695839 0.3339522 0.7064547
[4,] 1.0000000 2.0000000 3.0000000 4.0000000
[5,] 7.0000000 8.0000000 9.0000000 10.0000000
[,5]
[1,] 5.0000000
[2,] 0.5039326
[3,] 0.2315622
[4,] 5.0000000
[5,] 11.0000000
> unique(x)
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.0000000 3.0000000 4.0000000
[2,] 0.5647087 0.3511176 0.4897394 0.1811871
[3,] 0.5410171 0.0695839 0.3339522 0.7064547
[4,] 7.0000000 8.0000000 9.0000000 10.0000000
[,5]
[1,] 5.0000000
[2,] 0.5039326
[3,] 0.2315622
[4,] 11.0000000
> x
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.0000000 3.0000000 4.0000000
[2,] 0.5647087 0.3511176 0.4897394 0.1811871
[3,] 0.5410171 0.0695839 0.3339522 0.7064547
[4,] 1.0000000 2.0000000 3.0000000 4.0000000
[5,] 7.0000000 8.0000000 9.0000000 10.0000000
[,5]
[1,] 5.0000000
[2,] 0.5039326
[3,] 0.2315622
[4,] 5.0000000
[5,] 11.0000000
> x[!duplicated(x),]
[,1] [,2] [,3] [,4]
[1,] 1.0000000 2.0000000 3.0000000 4.0000000
[2,] 0.5647087 0.3511176 0.4897394 0.1811871
[3,] 0.5410171 0.0695839 0.3339522 0.7064547
[4,] 7.0000000 8.0000000 9.0000000 10.0000000
[,5]
[1,] 5.0000000
[2,] 0.5039326
[3,] 0.2315622
[4,] 11.0000000
> x=sample(1:1,20)
Error in sample.int(x, size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'
> x=sample(1:20,20)
> x
[1] 18 3 14 17 4 5 16 20 15 10 1 19 9 8 12 6
[17] 13 11 2 7
> x=c(1,1,1,1,1)
> x
[1] 1 1 1 1 1
> unique(x)
[1] 1
> z=list(1:3,Tom=c(1:2,a=list("R",letters[1:5]),w="hi!"))
> z
[[1]]
[1] 1 2 3
$Tom
$Tom[[1]]
[1] 1
$Tom[[2]]
[1] 2
$Tom$a1
[1] "R"
$Tom$a2
[1] "a" "b" "c" "d" "e"
$Tom$w
[1] "hi!"
> z[1]
[[1]]
[1] 1 2 3
> z[2]
$`Tom`
$`Tom`[[1]]
[1] 1
$`Tom`[[2]]
[1] 2
$`Tom`$a1
[1] "R"
$`Tom`$a2
[1] "a" "b" "c" "d" "e"
$`Tom`$w
[1] "hi!"
> z[[1]]
[1] 1 2 3
> z$Tom
[[1]]
[1] 1
[[2]]
[1] 2
$a1
[1] "R"
$a2
[1] "a" "b" "c" "d" "e"
$w
[1] "hi!"
> z$Tom$``
Error: attempt to use zero-length variable name
> z$Tom$a1
[1] "R"
> z$Tom[3]
$`a1`
[1] "R"
> z$Tom[[3]]
[1] "R"
> z
[[1]]
[1] 1 2 3
$Tom
$Tom[[1]]
[1] 1
$Tom[[2]]
[1] 2
$Tom$a1
[1] "R"
$Tom$a2
[1] "a" "b" "c" "d" "e"
$Tom$w
[1] "hi!"
> z$T$w
[1] "hi!"
> x=scan()
1: 3 3 3 4 1 4 2 1 3 2 5 3 1 2 5 2 3 4 2 2 5 3 1 4 2 2 4 3 5 2
31:
Read 30 items
> x
[1] 3 3 3 4 1 4 2 1 3 2 5 3 1 2 5 2 3 4 2 2 5 3 1 4
[25] 2 2 4 3 5 2
> barplot(x)
> table(x)
x
1 2 3 4 5
4 9 8 5 4
> barplot(table(x))
> barplot(table(x)/length(x))
> table(x)/length(x)
x
1 2 3
0.1333333 0.3000000 0.2666667
4 5
0.1666667 0.1333333
>attach(quine)
> table(age)
Error in table(age) : object 'age' not found
> table(Age)
Age
F0 F1 F2 F3
27 46 40 33
> table(Sex,Age)
Age
Sex F0 F1 F2 F3
F 10 32 19 19
M 17 14 21 14
> tab=xtabs(~Sex+Age,quine)
> unclass(tab)
Age
Sex F0 F1 F2 F3
F 10 32 19 19
M 17 14 21 14
attr(,"call")
xtabs(formula = ~Sex + Age, data = quine)
> tapply(Days,Age,mean)
F0 F1 F2 F3
14.85185 11.15217 21.05000 19.60606
> tapply(Days,list(Sex,Age),mean)
F0 F1 F2 F3
F 18.70000 12.96875 18.42105 14.00000
M 12.58824 7.00000 23.42857 27.21429
> detach(quine)
>
> ss=function(n=100){z=2;}
> for (i in 2:n)if(any(i%%2:(i-1)==0)==F)z=c(z,i);return(z) }
Error: unexpected '}' in "return(z) }"
> ss=function(n=100){z=2;}
> ss=function(n=100){z=2;for (i in 2:n)if(any(i%%2:(i-1)==0)==F)z=c(z,i);return(z) }
> ss
function(n=100){z=2;for (i in 2:n)if(any(i%%2:(i-1)==0)==F)z=c(z,i);return(z) }
> fix(ss)
> ss()
[1] 2 3 5 7 11 13 17 19 23 29 31 37 41
[14] 43 47 53 59 61 67 71 73 79 83 89 97
> t1.Sys.time()
Error in t1.Sys.time() : could not find function "t1.Sys.time"
> t1=Sys.time()
> ss(10000)
>Sys.time()-t1
> x=seq(-3,3,len=20)
> y=dnorm(x)
> x
[1] -3.0000000 -2.6842105 -2.3684211
[4] -2.0526316 -1.7368421 -1.4210526
[7] -1.1052632 -0.7894737 -0.4736842
[10] -0.1578947 0.1578947 0.4736842
[13] 0.7894737 1.1052632 1.4210526
[16] 1.7368421 2.0526316 2.3684211
[19] 2.6842105 3.0000000
> y
[1] 0.004431848 0.010873446 0.024145731
[4] 0.048529339 0.088279375 0.145346632
[7] 0.216591572 0.292125176 0.356604876
[10] 0.394000182 0.394000182 0.356604876
[13] 0.292125176 0.216591572 0.145346632
[16] 0.088279375 0.048529339 0.024145731
[19] 0.010873446 0.004431848
> w=data.frame(x,y)
> w
x y
1 -3.0000000 0.004431848
2 -2.6842105 0.010873446
3 -2.3684211 0.024145731
4 -2.0526316 0.048529339
5 -1.7368421 0.088279375
6 -1.4210526 0.145346632
7 -1.1052632 0.216591572
8 -0.7894737 0.292125176
9 -0.4736842 0.356604876
10 -0.1578947 0.394000182
11 0.1578947 0.394000182
12 0.4736842 0.356604876
13 0.7894737 0.292125176
14 1.1052632 0.216591572
15 1.4210526 0.145346632
16 1.7368421 0.088279375
17 2.0526316 0.048529339
18 2.3684211 0.024145731
19 2.6842105 0.010873446
20 3.0000000 0.004431848
> plot(y~x,w,main="正态密度函数")
> plot(y~x,w,type="l",main="正态密度函数")
> plot(y~x,w,type="o",main="正态密度函数")
> plot(y~x,w,type="b",main="正态密度函数")
> par(mfcol=c(1,1))
> plot(1,1,xlim=c(1,7.5),ylim=c(0,5),type="n")
> points(1:7,rep(4.5,7),cex=seq(1,4,l=7),col=1:7,pch=0:6)
> text(1:7,rep(3.5,7),labels = paste(0:6,letters[1:7]),cex=seq(1,4,l=7))
> text(1:7,rep(3.5,7),labels = paste(0:6,letters[1:7]),cex=seq(1,4,l=7),col=1:7)
> points(1:7,rep(2,7),pch=(0:6)+7)
> text((1:7)+0.25,rep(2,7),paste((0:6)+7))
> points(1:7,rep(1:7),pch=(0:6)+14)
> text((1:7)+0.25,rep(1:7),paste((0:6)+14))
> text((1:7)+0.25,rep(1:7),paste((0:6)+14))
> ?par