R语言入门学习笔记(2)

课程源代码:https://github.com/miwu8512/IntroToR

视频地址: https://www.youtube.com/watch?v=rPj5FsTRboE&list=PLBTcf4SwWEI9_kCOJ-1o-Jwr-_Qb6bkeg

Lecture 2 数据集的结构和作图的主要变量

1 understanding the dataset

1.1 Vector 向量

Define: Vectors are one-dimersional arrays that can hold nurmeric, character, logical data.
Function:

c()
a = c(1,2,-3,4)
b = c("one","two")
c = c(TRUE,TRUE,FALSE,TRUE)

1.2 Matrix 矩阵

Define: Matrix is a two-dimersional array where each element has th esame mode(nurmeric, character, or logical).
Function:

matrix() 

x = matrix (1:20,nrow=5,ncol=4,byrow=TRUE)

> x
      [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16
[5,]   17   18   19   20

x[2,]   #返回第二行数据
x[,2]   #返回第二列数据
x[1,4]
x[2,c(2,4)]
x[3:5,2]
rnames=c("apple","banana","orange","melon","corn")
cnames=c("cat","dog","bird","pig")
rownames(x)=rnames
colnames(x)=cnames

1.3 Array 数组

Define: Arrays are similar to matrics but can have more than two dimensions.
Function:

array()

dim1 = c("A1","A2")
dim2 = c("B1","B2","B3")
dim3 = c("C1","C2","C3","C4")
dim4 = c("D1","D2","D3") 
z = array(1:72,c(2,3,4,3), dimnames=list(dim1,dim2,dim3,dim4))

1.4 data frame

Define: A data frame is more general than a matrix in that different columns can cantain different modes of data(numeric, character, etc.). It is similar to the datasets you would typically see in SAS, SPSS, and Stata.

1.5 attach and detach

1.6 list

Define: Lists are the most complex of the R data types. A list allows you to gather a variety of (possible unrelated) objects under one name.

2 Graphs 作图

2.1 Graphical parameters

Define: customize many features of a graph (fonts,colors,axes,titles) through options called graphical parameters.
Function:

par() 

par(mfrow=c(2,2))  #设定多图布局网格
plot(rnorm(50),pch=19)          #pch:point的形状
plot(rnorm(20),type="l",lty=5)  #lty:line type 线的形状
plot(rnorm(100),cex=0.5)        #cex:放大比例
plot(rnorm(200),lwd=2)          #lwd: line width 粗细变换

2.2 title, axes, legends

title()  #标题
axis()   #坐标轴
legend() #图例

2.3 layout 布局

Define: layout() function has the form layout(mat) where mat is a matrix object specifying the location of the multiple plots to combine.

> attach(mtcars)
> layout(matrix(c(1,1,2,3),2,2,byrow= TRUE)) 
> hist(wt)
> hist(mpg)
> hist(disp)
> detach(mtcars)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,151评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,454评论 0 23
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,147评论 0 13
  • 环境塑造一个人的成长,而在两种不同教育中体验不一样的感受。 随着时间对于自身的体验,要做出转变通过对适应度的检测,...
    杨平的阅读 812评论 0 0
  • 他抛下重要的生意,奔进她的办公室,急急关上了门。她诧异,或许还有些许的恐惧,问他要做什么?他说来求婚。因觉得没必要...
    粉妖阅读 1,592评论 0 0