R 语言学习笔记一 :数据类型

> x<-1 #赋值
> x
[1] 1
> #上面[1]代表是x中第一个元素
> #数值型
> class(x) #查看x的类型
> #numeric表示存储的是数字
> x<-2L #赋值为整数
> class(x)
[1] "integer"
> #R对大小写敏感
> X<-1
> class(X)
[1] "numeric"
> #创建一个字符串类型:
> y<-"hello world"
> class(y)
[1] "character"
> #创建一个逻辑型的数据
> #注意大写真假
> t<-TRUE
> class(t)
[1] "logical"
> #创建一个复数值
> z<-1+2i
> class(z)
[1] "complex"
> 
> #向量Vercter
x<-vector("character",length = 10)

#简单创建向量,向量里有1 2 3 4四个元素
x1<-1:4

#第三个创建变量的方法
x2<-c(1,2,3,4)

#R会强制把不同类型的向量元素转化为相同的,以下全部转为字符型
x3<-c(TRUE,10,"a")

#显式强制转换
x4<-c("a","b","c")
as.numeric(x4)

#> as.numeric(x4)
#[1] NA NA NA
#Warning message:
#  NAs introduced by coercion 
#as.logical()
#as.character()

class(x4)

#给向量添加名称
names(x1)=c("a","b","c","d")
#> x1
#a b c d 
#1 2 3 4
#Matrix &Array

> x<-matrix(nrow = 3,ncol = 2)
> x
[,1] [,2]
[1,]   NA   NA
[2,]   NA   NA
[3,]   NA   NA
> x<-matrix(1:6,nrow = 3,ncol = 2)
> x
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
> #得到矩阵的纬度
> dim(x)
[1] 3 2
> #得到矩阵的属性
> attributes(x)
$dim
[1] 3 2

> #另外一种创建矩阵的方法
> y<-1:6
> y
[1] 1 2 3 4 5 6
> dim(y)<-c(2,3)
> y
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> 
> #矩阵的拼接
> y2<-matrix(1:6,nrow = 2,ncol = 3)
> #行拼接
> rbind(y,y2)
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[3,]    1    3    5
[4,]    2    4    6
> #列拼接
> cbind(y,y2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5    1    3    5
[2,]    2    4    6    2    4    6
> 
> 
> #数组:数组的纬度可以大于二
> x<-array(1:24,dim=c(4,6))
> x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    5    9   13   17   21
[2,]    2    6   10   14   18   22
[3,]    3    7   11   15   19   23
[4,]    4    8   12   16   20   24
> 
> #多维度
> x1<-array(1:24,dim=c(2,3,4))
> x1
, , 1

[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

[,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

[,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

[,1] [,2] [,3]
[1,]   19   21   23
[2,]   20   22   24

> 

#列表
#列表可以包含不同类型的变量
> listx<-list("a",2,10L,3+4i,TRUE)
> listx
[[1]]
[1] "a"

[[2]]
[1] 2

[[3]]
[1] 10

[[4]]
[1] 3+4i

[[5]]
[1] TRUE

> 

#注意列表中的=和<-不一样
  > listy<-list(a<-2,b<-3,c<-4)
  > listy
  [[1]]
  [1] 2

  [[2]]
  [1] 3

  [[3]]
  [1] 4

  > listy<-list(a=2,b=3,c=4)
  > listy
  $a
  [1] 2

  $b
  [1] 3

  $c
  [1] 4

  > listz<-list(c(1,2,3),c(4,5,6))
  > listz
  [[1]]
  [1] 1 2 3

  [[2]]
  [1] 4 5 6

  > 
 #纬度命名
> x<-matrix(1:6,nrow = 2,ncol = 3)
> dimnames(x)<-list(c("a","b"),c("c","d","e"))
> x
c d e
a 1 3 5
b 2 4 6
> 

#因子factor
#假如因子里面包含三个女性两个男性
> x<-factor(c("female","female","male","male","female"))
> x
[1] female female male   male   female
Levels: female male
> 
#设定基线水平---很重要
  #列子:levels(y) <- c("low", "high")

> y<-factor(c("female","female","male","male","female")) 
> levels(y)<-c("male","female")
> y
[1] male   male   female female male  
Levels: male female
> 

  > table(y)
y
male female 
3      2 
> 
  > unclass(y)
[1] 1 1 2 2 1
attr(,"levels")
[1] "male"   "female"
>  
  > class(y)
[1] "factor"
>    
#缺失值
#--NA/NaN属于NA,
> x<-c(1,NA,2,NA,3)
> is.na(x)
[1] FALSE  TRUE FALSE  TRUE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE

> x<-c(1,NaN,2,NA,3)
> is.na(x)
[1] FALSE  TRUE FALSE  TRUE FALSE
> is.nan(x)
[1] FALSE  TRUE FALSE FALSE FALSE
>
#数据框 date frame
#按列搞
> df<-data.frame(id=c(1,2,3,4),name=c("a","b","c","d"),gender=c(TRUE,TRUE,FALSE,FALSE))
> df
id name gender
1  1    a   TRUE
2  2    b   TRUE
3  3    c  FALSE
4  4    d  FALSE

> nrow(df)
[1] 4
> ncol(df)
[1] 3
> 
  > df2<-data.frame(id=c(1,2,3,4),score=c(80,86,90,100))
> df2
id score
1  1    80
2  2    86
3  3    90
4  4   100
> #转化为矩阵
  > data.matrix(df2)
id score
[1,]  1    80
[2,]  2    86
[3,]  3    90
[4,]  4   100
> 

#日期和时间(date,time)
#获取当前时间
> x<-date()
> x
[1] "Thu Feb 18 15:12:14 2016"
> class(x)
[1] "character"
> x2<-Sys.Date()
> x2
[1] "2016-02-18"
> class(x2)

#存储时间
> x3<-as.Date("2016-09-01")
> x3
[1] "2016-09-01"
> class(x3)
[1] "Date"
> weekdays(x3)
[1] "星期四"
> months(x3)
[1] "九月"
#季度
> quarters(x3)
[1] "Q3"
> 
#求日期距离1970-01-01过了多少天
> julian(x3)
[1] 17045
attr(,"origin")
[1] "1970-01-01"
>  

 #时间的运算
  > x4<-as.Date("2016-10-19")
> x4-x3
Time difference of 48 days
> as.numeric(x4-x3)
[1] 48
> 
#时间:POSIXct/POSIXlt  
  #距离1970-01-01的秒数/Sys.time()
  #POSIXct:整数,常用于存入数据框
  #POSIXlt:列表,包括星期,年月日等信息  

  > x<-Sys.time()
> x
[1] "2016-02-18 15:26:13 CST"
> class(x)
[1] "POSIXct" "POSIXt" 
> p<-as.POSIXlt(x)
> p
[1] "2016-02-18 15:26:13 CST"
> class(p)
[1] "POSIXlt" "POSIXt" 
> 
  > names(unclass(p))
[1] "sec"    "min"    "hour"   "mday"   "mon"   
[6] "year"   "wday"   "yday"   "isdst"  "zone"  
[11] "gmtoff"
> p$sec
[1] 13.07167
> as.POSIXct(p)
[1] "2016-02-18 15:26:13 CST"
> #取出名字
  > names(unclass)
NULL
> names(unclass(p))
[1] "sec"    "min"    "hour"   "mday"   "mon"   
[6] "year"   "wday"   "yday"   "isdst"  "zone"  
[11] "gmtoff"
> p$hour
[1] 15 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值