One

#install package
install.packages("ggplot2") #load library
library(ggplot2)
#update.packages() #vector
v=c(1,4,4,3,2,2,3)
#get vector elements, index is 2,3,4
v[c(2,3,4)]
#get vector elements, index range is 2 to 4
v[2:4]
#get vector elements, index is 2,4,3
v[c(2,4,3)]
#delete vector elements index is 2
v[-2]
#delete vector elements index range is 2 to 4
v[-2:-4]
#get vector elements, which value < 3
v[v<3]
#get vector elements index, which value = 3
which(v==3)
#get vector max element index
which.max(v)
#get vector min element index
which.min(v) #get random values
set.seed(250)
a=runif(3,min = 0, max = 100) floor(a)
ceiling(a)
round(a, 3) #read data from file
data1=read.table(file = "/Users/hwgt/workspace/Rlang/data_1.txt", header = TRUE)
#header to variable
attach(data1) #draw
set.seed(123)
x=rnorm(100, mean = 100, sd = 10)
set.seed(234)
y=rnorm(100, mean = 100, sd = 10)
hist(x, breaks = 20)
plot(density(x))
plot(x)
boxplot(x, y)
boxplot(yage)
qqnorm(x)
qqline(x)
qqplot(x, y)

Two

# vector
a = c(1, 2, 3, 4, 5)
b = c("one", "two", "three")
c = c(TRUE, FALSE) # matrix
x = matrix(1:20, nrow = 5, ncol = 4, byrow = TRUE)
x
y = matrix(1:20, nrow = 5, ncol = 4, byrow = FALSE)
y 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")
m = matrix(1:20, 5, 4, TRUE)
rownames(m) = rnames
colnames(m) = cnames
m 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))
z
z[1,2,3,] # data frame
patientID = c(1,2,3,4)
age = c(25,26,27,28)
diabetes = c("Type1", "Type2", "Type1", "Type2")
status = c("Poor", "Improved", "Excellent", "Poor")
patientData = data.frame(patientID, age, diabetes, status)
patientData # list
listData = list(patientData, x)
listData[1]
listData[2] # graphs
par(mfrow=c(2,2))
plot(rnorm(50), pch=17)
plot(rnorm(20), type="l", lty=5)
plot(rnorm(100), cex=0.5)
plot(rnorm(200), lwd=2)

Three

# operator

# control flow
# for loop
for(a in 1:10) {
print(a)
} # while loop
i = 1
while(i <= 10) {
print(i)
i = i + 1
} # if
i = 2
if (i == 1) {
print("hello r")
} else if(i == 2){
print("goodbye r")
} else {
print("good r")
} # switch
feelings = c("sad", "afraid")
for(i in feelings) {
print(
switch(i,
a = "a",
sad = "b",
c = "c",
afraid = "d",
e = "e")
)
}
# function
numSum = function(a, b) {
return(a + b)
}
print(numSum(1,2))

Four

# Bar Chart
#install.packages("vcd")
library(vcd)
counts = table(Arthritis$Improved)
counts
par(mfrow=c(2,2)) barplot(counts,
main="Simple Bar Plot",
xlab = "Improvement",
ylab = "Frequency") barplot(counts,
main="Horizontal Bar Plot",
xlab = "Frequency",
ylab = "Improvement",
horiz = TRUE) counts <- table(Arthritis$Improved, Arthritis$Treatment)
counts barplot(counts,
main="Stacked Bar Plot",
xlab = "Treatment",
ylab = "Frequency",
col = c("red", "yellow", "green"),
legend = rownames(counts)) barplot(counts,
main="Grouped Bar Plot",
xlab = "Treatment",
ylab = "Frequency",
col = c("red", "yellow", "green"),
legend = rownames(counts),
beside = TRUE) # Pie Chart
#install.packages("plotrix")
library(plotrix)
par(mfrow=c(2,2))
slices <- c(10,12,4,16,8)
lbls <- c("US", "UK", "AU", "GE", "FR")
pie(slices, lbls, main = "Simple Pie Chart", edges = 300, radius = 1) pct <- round(slices/sum(slices)*100)
lbls2 <- paste(lbls, " ", pct, "%", sep = " ")
pie(slices, lbls2, main = "Simple Pie Chart With Progress", edges = 300, radius = 1) pie3D(slices,labels = lbls, explode = 0.1, main = "3D Pie Chart", edges = 300, radius = 1) # Fan Plot
slices3 = c(10, 12, 4, 16, 8)
lbls3 = c("US", "UK", "DE", "KR", "CN")
fan.plot(slices3, labels = lbls3, main = "Fan Plot") # Dot Chart
dotchart(mtcars$mpg, labels = row.names(mtcars), cex = 0.7,main = "Dot Chart", xlab = "Miles Per Gallon") # Summary
head(mtcars)
summary(mtcars) # Tables
attach(mtcars)
table(cyl)
summary(mpg)
table(cut(mpg, seq(10, 34, by=2))) # Correlations
states = state.x77[, 1:6]
cov(states)
var(states)
cor(states) # T Test
x = rnorm(100, mean = 0, sd = 1)
y = rnorm(100, mean = 30, sd = 1)
t.test(x, y, alt="two.sided", paired = TRUE) # Wilcoxon
wilcox.test(x, y, alt="less")

R Language Learn Notes的更多相关文章

  1. String comparison is too slow in R language

    ## String comparison is too slow in R language ## it will take 3 minutes, it is too slow date() strA ...

  2. Linux Academy Learn Notes

    Linux Essentials Certification Globbing ls ?.txt --- ? stands for one character while * means one or ...

  3. Java 8 Learn Notes

    Main reference: [1] http://winterbe.com/posts/2014/03/16/java-8-tutorial/ [2] https://plus.google.co ...

  4. R Language

    向量定义:x1 = c(1,2,3); x2 = c(1:100) 类型显示:mode(x1) 向量长度:length(x2) 向量元素显示:x1[c(1,2,3)] 多维向量:multi-dimen ...

  5. Bash Scripting Learn Notes

    References: [1] http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ 1. Executing programs from a scri ...

  6. Java 8 Learn Notes - Streams

    Main reference [1] http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples 1. How Stream ...

  7. Dart Learn Notes 04

    流程控制语句 流程控制语句的作用就是控制代码的执行流程. if and else var a = 10; if(a > 10){ print('ok'); }else if( 5 < a ...

  8. Dart Learn Notes 03

    操作符 dart 有一套自己定义的操作符: 这里我就不再写了,直接copy一份官网的. 如果有过编程基础,上边展示的操作符应该都不陌生. 算术运算符 加: + 减: - 乘: * 除: / 取余: % ...

  9. Dart Learn Notes 02

    Functions Dart是一门面向对象的语言,所以即便是方法也是一个对象,它的类型是Function. 这就意味着方法可以指向变量,也可以作为方法中的参数供其他方法使用.甚至可以让 一个类作为一个 ...

随机推荐

  1. bbs论坛浏览器兼容性问题

    一直都是在chrome上进行调试,今天终于把bbs论坛这个项目搭建完了,进入IE.Firefox看了看 吓哭了!!! 火狐 Edge chrome 特别是加了<!DOCTYPE html> ...

  2. [Java123] JDBC and Multi-Threading 多线程编程学习笔记

    项目实际需求:DB交互使用多线程实现 多线程编程基础:1.5  :( (假设总分10) 计划一个半月从头学习梳理Java多线程编程基础以及Oracle数据库交互相关的多线程实现 学习如何通过代码去验证 ...

  3. textarea多行文本框自适应高度

    <script src="https://cdn.bootcss.com/jquery/1.8.1/jquery.js"></script> <scr ...

  4. Apollo2.5摄像头安装

    前言:在Apollo美研团队和长沙CiDi团队的支持下,最近完成了Apollo推荐的摄像头AR023ZWDR(Rev663F12)调试,在这里对Apollo的笔记做一个补充,希望以后的开发者不用在踩我 ...

  5. 第2章 K近邻算法

    numpy中的tile函数: 遇到numpy.tile(A,(b,c))函数,重复复制A,按照行方向b次,列方向c次. >>> import numpy >>> n ...

  6. Windows命令行使用总结(持续更新)

    1. 根据端口号查找进程: netstat -ano | findstr "port" 2. 获取任务列表: tasklist 3. 如果我们想要结束某个程序的所有进程的话,比如, ...

  7. 404 Note Found 队-Alpha4

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:何家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员:何宇恒 展示组内最新 ...

  8. Spring Cloud Sleuth 之Greenwich版本全攻略

    微服务架构是一个分布式架构,微服务系统按业务划分服务单元,一个微服务系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性较高,如果出现了错误和异常,很难去定位.主要体现在一个请求可能需要调用很 ...

  9. Notes 20180310 : String第二讲_String的声明与创建

    1  字符串的声明与创建 学习String的第一步就是创建(声明)字符串,我们在这里之所以分为创建和声明(其实是一个意思,都是创建字符串,但两者却有本质的区别)是因为String是一个很特殊的类,它的 ...

  10. win 10 如何关闭自动更新

    1.右键“此电脑”图标,点击“管理”,打开“计算机管理”窗口; 2.在“计算机管理”窗口中找到“服务和应用程序”,点击“服务”,打开“服务”窗口; 3.在“服务”中找到“Windows Update” ...