-
R的数据结构 1)5中基本对象类型(Classes of Objects) -> 查看对象类型:class(x) 去掉对象属性:unclass(x) - 字符(character):y <- "hello world" - 数值(numeric:real numbers 可以是整数也可以是小数) - 整数(integer):x <- 2L 【L代表整数】 - 复数(complex:1+2i) - 逻辑(logical:True or False): t <- TRUE 【TRUE FALSE均要大写】 如何创建对象并赋值 赋值符号“<-”,即 x <- 1 【中间均有空格】 数据属性(Attribute) - 名称(name) - 维度(dimensions:matrix,array) - 类型(class) - 长度(length)查看全部
-
which函数就是判断里面的内容是否符合条件查看全部
-
#tapply > x<-c(rnorm(5),runif(5),rnorm(5,1)) > f<-gl(3,5) > f [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 Levels: 1 2 3 > x [1] 1.54937342 0.02209166 0.42718271 1.33836257 0.61729972 0.67136483 [7] 0.80328042 0.04200875 0.77180942 0.34568232 0.21351360 -1.12589708 [13] 0.85513753 1.21234216 1.10203385 > tapply(x,f,mean) 1 2 3 0.7908620 0.5268292 0.4514260 > tapply(x,f,mean,simplify=FALSE) $`1` [1] 0.790862 $`2` [1] 0.5268292 $`3` [1] 0.451426查看全部
-
#mapply > list(rep(1,4),rep(2,3),rep(3,2),rep(4,1)) [[1]] [1] 1 1 1 1 [[2]] [1] 2 2 2 [[3]] [1] 3 3 [[4]] [1] 4 > mapply(rep,1:4,4:1) [[1]] [1] 1 1 1 1 [[2]] [1] 2 2 2 [[3]] [1] 3 3 [[4]] [1] 4 > s<-function(n,mean,std){rnorm(n,mean,std)} > s(4,0,1) [1] 0.7450995 1.2434864 0.8101944 -0.3404109 > > mapply(s,1:5,5:1,2) [[1]] [1] 5.01628 [[2]] [1] 6.478765 3.329168 [[3]] [1] 2.805968 1.337542 4.637929 [[4]] [1] -0.3507264 3.6066690 0.2904657 6.9470794 [[5]] [1] -1.5605870 4.1581596 1.5869184 0.5890816 -0.4222614 > #第一个元素 均值为查看全部
-
> x<-array(rnorm(2*3*4),c(2,3,4)) > x , , 1 [,1] [,2] [,3] [1,] 1.1956873 -0.2596583 -1.2015969 [2,] -0.3209505 0.3053011 -0.7746961 , , 2 [,1] [,2] [,3] [1,] 0.1231171 -0.6763505 0.06413046 [2,] -1.7514369 -0.1743128 0.24746294 , , 3 [,1] [,2] [,3] [1,] -1.493169 -0.8187093 -1.2907699 [2,] 1.214228 1.2506630 -0.6219673 , , 4 [,1] [,2] [,3] [1,] 1.7280836 0.4591249 -0.05287132 [2,] -0.7977063 -0.3984747 -0.09035234 > apply(x,c(1,2),mean) [,1] [,2] [,3] [1,] 0.3884298 -0.3238983 -0.6202769 [2,] -0.4139665 0.2457941 -0.3098882 > #在第一维和二维组成的平面上,沿着第四维压下去查看全部
-
#apply > x<-matrix(1:16,4,4) > apply(x,2,mean) [1] 2.5 6.5 10.5 14.5 > #求列的平均 > > apply(x,2,sum) [1] 10 26 42 58 > rowSums(x) [1] 28 32 36 40 > x<-matrix(rnorm(100),10,10) > apply(x,1,quantile,probs=c(0.25,0.75)) [,1] [,2] [,3] [,4] [,5] [,6] 25% -0.563974214 -1.10145180 -0.2492682 -0.4203206 -0.7928294 -0.3024561 75% -0.004877207 -0.08161928 0.4307766 0.9236806 1.5585471 0.1883218 [,7] [,8] [,9] [,10] 25% -1.0048741 -0.204027 -0.2934863 -0.09809754 75% 0.3582009 1.078703 0.7138151 0.32390695 > #随机从正态分布总体抽取100个数据,10行10列的矩阵 > #行,函数名,对quantile(百分位点)限制查看全部
-
apply查看全部
-
重要函数的使用 #lapply > str(lapply) function (X, FUN, ...) > x<-list(a=1:10,b=c(11,21,31,41,51)) > lapply(x,mean) $a [1] 5.5 $b [1] 31 > #a、b元素的平均值分别为5.5、31 > > x<-1:4 > lapply(x,runif) [[1]] [1] 0.7459708 [[2]] [1] 0.4013926 0.3470519 [[3]] [1] 0.5849431 0.1393646 0.5620479 [[4]] [1] 0.53573261 0.22635634 0.02265272 0.96663953 > lapply(x,runif,min=0,max=100) [[1]] [1] 74.25802 [[2]] [1] 33.48409 10.44494 [[3]] [1] 48.67692 85.32088 81.56280 [[4]] [1] 51.426180 6.028272 16.220745 29.223361 > x<-list(a=matrix(1:6,2,3),b=matrix(4:7,2,2)) > lapply(x,function(m) m[1,]) $a [1] 1 3 5 $b [1] 4 6 > #第一个、二个矩阵中的第一行 > > x<-list(a=1:10,b=c(11,21,31,41,51)) > lapply(x,mean) $a [1] 5.5 $b [1] 31 > sapply(x,mean) a b 5.5 31.0查看全部
-
重要函数的使用查看全部
-
构建子集查看全部
-
> x<-1:5 > y<-6:10 > > x+y [1] 7 9 11 13 15 > #五个对应元素依次相加 > > x<-matrix(1:4,nrow=2,ncol=2) > y<-matrix(rep(2,4),nrow=2,ncol=2) > x [,1] [,2] [1,] 1 3 [2,] 2 4 > y [,1] [,2] [1,] 2 2 [2,] 2 2 > #rep(2,4) 把2重复4次 > > x*y [,1] [,2] [1,] 2 6 [2,] 4 8 > #x和y对应的元素相乘 > > x %*% y [,1] [,2] [1,] 8 8 [2,] 12 12 > #矩阵乘查看全部
-
> library(datasets) > head(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 > g<-complete.cases(airquality) > g [1] TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE > airquality[g,][1:10,] 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 7 23 299 8.6 65 5 7 8 19 99 13.8 59 5 8 9 8 19 20.1 61 5 9 12 16 256 9.7 69 5 12 13 11 290 9.2 66 5 13 14 14 274 10.9 68 5 14 > #缺少第六行、十行、十一行的数据,说明这三行存在缺失值查看全部
-
这是之前学过的内容了,再看一遍巩固一下查看全部
-
处理缺失值 > x<-c(1,NA,2,NA,3) > is.na(x) [1] FALSE TRUE FALSE TRUE FALSE > #是缺失值 > > x[!is.na(x)] [1] 1 2 3 > #!取反,拿到不是缺失值的元素 > y<-c("a","b","NA","c","NA") > z<-complete.cases(x,y) > z [1] TRUE FALSE TRUE FALSE TRUE > x[z] [1] 1 2 3 > y[z] [1] "a" "NA" "NA"查看全部
-
> #列表的不完全匹配 > l<-list(asdfg=1:10) > l$asdfg [1] 1 2 3 4 5 6 7 8 9 10 > l$a [1] 1 2 3 4 5 6 7 8 9 10 > l[["a"],exact=FALSE] 错误: 意外的',' in "l[["a"]," > l[["a",exact=FALSE]] [1] 1 2 3 4 5 6 7 8 9 10 > #关掉精确匹配 > > x<-list(asdfghj=1:10,b=1:2,aaa=3:5) > x$a NULL > x[["a",exact=FALSE]] NULL > x$as [1] 1 2 3 4 5 6 7 8 9 10 >查看全部
举报
0/150
提交
取消