几个函数的问题
- 逻辑判断输出
> r1 <- c('a','b',d,23,24,365)
> r1
[1] "a" "b" "22" "23" "24" "365"
> class(r1)
[1] "character"
> r2 <- as.numeric(r1)
Warning message:
NAs introduced by coercion
> r2
[1] NA NA 22 23 24 365
> e <- as.logical(r2) #此处在做逻辑判断时:1.系统有默认的判断标准来返回T/F?
##2.可否同时设定判断条件,比如原值NA的仍返回NA,非NA有数值的, >23.5=TURE, <23.5=返回FALSE
> e
[1] NA NA TRUE TRUE TRUE TRUE
> f <- as.numeric(e) ##数据的二分类转换?
> f
[1] NA NA 1 1 1 1
- collapse = NULL
#paste (..., sep = " ", collapse = NULL)
#paste0(..., collapse = NULL) #'collapse = NULL' 是函数的一部分,不能赋值,NULL不能更改,删掉也没有影响,则collapse有什么意义?
- set.seed()与 '!'运算
> a<-c(TRUE,FALSE)
> set.seed(0512) #同向量a有什么关系呢
> ###sample是随机抽样的函数,这里从a中的TRUE和FALSE中随机有放回抽取
> tmp<-sample(a,5,replace = TRUE)
> tmp
[1] TRUE TRUE TRUE FALSE FALSE
> !tmp
[1] FALSE FALSE FALSE TRUE TRUE
###while
- 常量
> ##常量
> e<-month.name[c(1:3,2,5)]
> e
[1] "January" "February" "March" "February" "May"
> pi
[1] 3.141593 #what if给系统指定一些数学常量,比如自然对数e,欧拉常数γ等?
#what if
- 运算符索引
> e<-LETTERS[1:24]
> dim(e)<-c(6,4)
> e
[,1] [,2] [,3] [,4]
[1,] "A" "G" "M" "S"
[2,] "B" "H" "N" "T"
[3,] "C" "I" "O" "U"
[4,] "D" "J" "P" "V"
[5,] "E" "K" "Q" "W"
[6,] "F" "L" "R" "X"
获取e的奇数行内容
#1.逻辑+循环补齐
> t <- c(T,F)
> e[t,]
[,1] [,2] [,3] [,4]
[1,] "A" "G" "M" "S"
[2,] "C" "I" "O" "U"
[3,] "E" "K" "Q" "W"
#2.行数求余
> e[c(nrow(e)%%2==1), ] #为啥注释变不成蓝字了?
[,1] [,2] [,3] [,4] #nrow=6,此处逻辑运算不对,但如何表示第几行这个顺序呢?
#如rownames命名1-6,则1-6变为charactor,还是不能进行%%2的操作?
- 最后终于陷入一片混乱[doge][doge][doge]
> April
date plan color
1 21 mon green
2 22 tue red
3 23 wed white
4 35 thur black
5 52 fri purple
> dim(April)
[1] 5 3
> str(April)
'data.frame': 5 obs. of 3 variables:
$ date : num 21 22 23 35 52
$ plan : chr "mon" "tue" "wed" "thur" ...
$ color: chr "green" "red" "white" "black" ...
> typeof(April)
[1] "list" #####这个list不是很理解
> factor(April)
date
c(21, 22, 23, 35, 52)
plan
c("mon", "tue", "wed", "thur", "fri")
color
c("green", "red", "white", "black", "purple")
3 Levels: c(21, 22, 23, 35, 52) ... c("green", "red", "white", "black", "purple")
> class(April)
[1] "data.frame"
#####定义一个f
> f <- April[c(2:4),c(1,2)]
> f
date plan
2 22 tue
3 23 wed
4 35 thur
> dim(f)
[1] 3 2
> str(f)
'data.frame': 3 obs. of 2 variables:
$ date: num 22 23 35
$ plan: chr "tue" "wed" "thur"
> typeof(f)
[1] "list"
> factor(f)
date plan
c(22, 23, 35) c("tue", "wed", "thur")
Levels: c(22, 23, 35) c("tue", "wed", "thur")
> class(f)
[1] "data.frame"
#####定义一个e
> e <- c(April[c(2:4),2])
> e
[1] "tue" "wed" "thur"
> dim(e)
NULL
> str(e)
chr [1:3] "tue" "wed" "thur"
> typeof(e)
[1] "character"
> factor(e)
[1] tue wed thur
Levels: thur tue wed
> class(e)
[1] "character"
#####定义一个g
> g <- April[c(2:4),2]
> dim(g)
NULL
> str(g)
chr [1:3] "tue" "wed" "thur"
> typeof(g)
[1] "character"
> factor(g)
[1] tue wed thur
Levels: thur tue wed
> class(g)
[1] "character"
#####April[c(2:4),2]前加不加c,输出都是一个向量?
#####以及
> h <- matrix(f) h啥参数都没给呢还,怎么就能是matrix了?
> i <- matrix(f,nrow=3,ncol=2) 当f是vector,则i的定义没有问题。所以这里的主要问题是因f是个dataframe?since它的type是list,如何让f变vector?
> h
[,1]
[1,] Numeric,3
[2,] Character,3
> i
[,1] [,2]
[1,] Numeric,3 Character,3
[2,] Character,3 Numeric,3
[3,] Numeric,3 Character,3
> class(h)
[1] "matrix"
> class(i)
[1] "matrix"