R 语言学习一


0x01 hello 2020

2020,有一个全新的开始;2020,你还好么?

1、定义与赋值、输出

R 语言的变量的定义与赋值需要用到符号:“<-”(右赋给左)或 “->”(左赋给右) 或 者 “=”(右赋给左)
输出print()或cat():

> firstString <- "hello 2020"
> print(firstString)

在这里插入图片描述

2、注释

单行注释:#,不支持多行注释

# 2020, are you ready?

多行注释技巧:

if(FALSE){""}	

0x02 数据类型

数据类型数据实例
logicalTRUE、FALSE> v <- TRUE
> print(v)
[1] TRUE
> print(class(v))
[1] “logical”
numeric52134> v <- 52134
> print(v)
[1] 52134
> print(class(v))
[1] “numeric”
integer444L> v <- 4444L
> print(v)
[1] 4444
> print(class(v))
[1] “integer”
complex5 + 2i> v <- 5 + 2i
> print(v)
[1] 5+2i
> print(class(v))
[1] “complex”
characterdying> v <- “dying”
> print(v)
[1] “dying”
> print(class(v))
[1] “character”
rawdying 存储:64 79 69 6e 67> v <- charToRaw(“dying”)
> print(v)
[1] 64 79 69 6e 67
> print(class(v))
[1] “raw”

0x03 向量 vectors

使用函数**c()**产生向量:

> v <- c("tom","jerry","killer")
> print(v)
[1] "tom"    "jerry"  "killer"
> print(class(v))
[1] "character"

0x04 列表 list

列表是一个R对象,它可以在其中包含许多不同类型的元素,如向量,函数甚至其中的另一个列表。

> list1 <- list(c(2,5,3),21.3,cos)
> print(list1)
[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x)  .Primitive("cos")

> print(class(list1))
[1] "list"

0x04 矩阵 matrices

> m <- matrix( c('a','b','c','1','2','3'), nrow = 2, byrow = TRUE)
> print(m)
	[,1] [,2] [,3]
[1,] "a"  "b"  "c" 
[2,] "1"  "2"  "3" 
> m <- matrix( c('a','b','c','1','2','3'), nrow = 3, byrow = TRUE)
> print(m)
	[,1] [,2]
[1,] "a"  "b" 
[2,] "c"  "1" 
[3,] "2"  "3" 

0x05 数组 arrays

虽然矩阵被限制为二维,但阵列可以具有任何数量的维度。 数组函数使用一个dim属性创建所需的维数。dim=c(x,y,z)表示x*y的矩阵,z表示矩阵个数,由输出的结果,R语言数据存储应该也是列优先

> a <- array(c('tom','jerry','killer'), dim = c(2,2,2))
> print(a)
, , 1

     [,1]    [,2]    
[1,] "tom"   "killer"
[2,] "jerry" "tom"   

, , 2

     [,1]     [,2]   
[1,] "jerry"  "tom"  
[2,] "killer" "jerry"

> 
> a <- array(c('tom','jerry','killer'), dim = c(3,3,3))
> print(a)
, , 1

     [,1]     [,2]     [,3]    
[1,] "tom"    "tom"    "tom"   
[2,] "jerry"  "jerry"  "jerry" 
[3,] "killer" "killer" "killer"

, , 2

     [,1]     [,2]     [,3]    
[1,] "tom"    "tom"    "tom"   
[2,] "jerry"  "jerry"  "jerry" 
[3,] "killer" "killer" "killer"

, , 3

     [,1]     [,2]     [,3]    
[1,] "tom"    "tom"    "tom"   
[2,] "jerry"  "jerry"  "jerry" 
[3,] "killer" "killer" "killer"

0x06 因子 factors

因子是使用向量创建的r对象。 它将向量与向量中元素的不同值一起存储为标签。 标签总是字符,不管它在输入向量中是数字还是字符或布尔等。 它们在统计建模中非常有用。
使用factor()函数创建因子。nlevels函数给出级别计数。

> apple_colors <- c('green','green','yellow','red','red','red','green')
> factor_apple <- factor(apple_colors)
> print(factor_apple)
[1] green  green  yellow red    red    red    green 
Levels: green red yellow
# 统计个数,按类别排序
> print(nlevels(factor_apple))
[1] 3
# 共有多少种类别

0x07 数据帧 Data Frame

数据帧是表格数据对象。 与数据帧中的矩阵不同,每列可以包含不同的数据模式。 第一列可以是数字,而第二列可以是字符,第三列可以是逻辑的。 它是等长度向量列表
使用data.frame()函数创建数据帧。

BMI <- data.frame(
  gender = c("male","female","unknown"),
  height = c(155,156,157),
  wight = c(55,66,77),
  age = c(1,2,3)
)
print(BMI)

output:
在这里插入图片描述

0x08 变量的操作

1、查看变量

使用**ls()**查看环境中使用的变量:

> print(ls())
 [1] "a"            "apple"        "apple_colors" "assets.df"    "BMI"          "c"           
 [7] "factor_apple" "firstString"  "list1"        "m"            "string"       "test.df"     
[13] "v"            "V"           

匹配变量名:ls( pattern = “”)

> print(ls(pattern = "apple"))
[1] "apple"        "apple_colors" "factor_apple"

以点(.)开头的变量被隐藏,它们可以使用ls()函数的“all.names = TRUE”参数列出。

> print(ls(all.names = TRUE))
 [1] ".Random.seed" "a"            "apple"        "apple_colors" "assets.df"    "BMI"         
 [7] "c"            "factor_apple" "firstString"  "list1"        "m"            "string"      
[13] "test.df"      "v"            "V"        

2、删除变量

删除单个变量,使用rm()

rm(V)

删除所有变量,使用rm(list = ls())

rm(list = ls())

今日推荐《夏艺寒-世间美好》
歌手:夏艺寒
专辑:世间美好

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值