`
sillycat
  • 浏览: 2487240 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Rlang(2)Diagram - Model - Loop - Ifelse

 
阅读更多
Rlang(2)Diagram - Model - Loop - Ifelse

1. Generate the Diagram
This will draw the diagram for us.
> plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5", type = "l", ylim = c(0, 200))
> lines(x = pm$time, y = pm$h100, col = "red")
> legend(x = 15, y = 180, legend = c("8 m", "100 m"), col = c("black", "red"), lty = 1)

plot, lines, legend.

These command will open the file and draw the things on the file.
pdf(file ="/opt/data/output1.pdf")
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5", type = "l", ylim = c(0, 200))
lines(x = pm$time, y = pm$h100, col = "red")
legend(x = 15, y = 180, legend = c("8 m", "100 m"), col = c("black", "red"), lty = 1)
dev.off()

Some complex draw example
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5 at 8 m", type = "l", ylim = c(0, 200), axes=FALSE)
axis(2)
axis(4)
axis(1, at = 0 : 23, labels = 0 : 23)
points(x = pm$time, y = pm$h100, col = "red", type = "l")
points(x = pm$time, y = pm$h325, col = "blue", type = "l")
abline(h = c(10, 15 , 25, 35), col = "grey", lty = 2)
legend("top", legend = c("8 m", "100 m", "325 m"), col = c("red", "black", "blue"), lty = 1)
box() #

Some function I need to be familiar with. plot, box plot, points(), lines(), lines(), abline(), box(), axis(), legend(), pdf()

2. Modeling
Functions: lm(), nls(), par()
Linear Regression

https://zh.wikipedia.org/zh-cn/%E7%B7%9A%E6%80%A7%E5%9B%9E%E6%AD%B8

http://www.jasongj.com/2015/03/27/ml1_linear_regression/

loss function, error function, cost function

Draw the 4 Plot
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
pm <- read.csv(file = mydata)
m <- lm(pm$h100 ~ pm$h8)
summary(m)
par(mfrow = c(2,2))
plot(m)

Draw the Line and All the Points
mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
pm <- read.csv(file = mydata)
m <- lm(pm$h100 ~ pm$h8)

summary(m)

par(mfrow = c(1, 1))
plot(pm$h8, pm$h100, cex = 2, pch = 21, bg = "red", col = "green")
abline(m, col = "purple", lwd = 3)
legend("bottomright", pch = c(21, NA), lty = c(NA, 1), legend = c("Data", "Linear fit"),
       pt.bg = "red", col = c("green", "purple"), lwd = c(NA, 2))

3. Loop
Draw three plots, based on the different high of the data
> par(mfrow=c(1,3))
> plot(pm[,1], pm[,2], cex=2, type = "l")
> plot(pm[,1], pm[,3], cex=2, type = "l")
> plot(pm[,1], pm[,4], cex=2, type = "l")

The for Loop
par(mfrow=c(1,3))
for (i in c(2, 3, 4)) plot(pm[, 1], pm[, i], cex = 2, type = "l")

Some other useful functions are : for(), while(), until()

() for function, [] for the index of array or seq, {} for code block.

4. Check Statements
[1] FALSE
> 3>2 | 1 > 2
[1] TRUE
> 3>2&!(1>2)
[1] TRUE

That is really powerful
> x <- 1:3
> x == 2
[1] FALSE  TRUE FALSE

Define 2 array of numbers
> x <- 6:1
> x
[1] 6 5 4 3 2 1
> y <-c(3,5,7)
> y
[1] 3 5 7

Check if x is in y
> x %in% y
[1] FALSE  TRUE FALSE  TRUE FALSE FALSE

Find the number in x and also in y
> x[x %in% y]
[1] 5 3

Find their index
> which(x %in% y)
[1] 2 4

Find the max value exist in which time
> mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
> pm <- read.csv(file = mydata)
> pm$time[which(pm$h8 == max(pm$h8))]
[1]  8  9 10

Print is also a useful command in R language
> x <- 12
> if(x<99) print("x is less than 99")
[1] "x is less than 99"

Useful Command ifelse
> x <- 12
> if (x < 99) {
+   print("x is less than 99")
+ } else {
+   print("x is larger than 99")
+ }
[1] "x is less than 99"
> ifelse(x < 99, "small", "large")
[1] "small"

IFELSE can be powerful used in a lot of place
plot(x = pm$time, y = pm$h8, xlab = "Time", ylab = "PM2.5",
     cex = ifelse(pm$h8 == max(pm$h8), 2, 1),
     pch = 16, type = "b",
     col = ifelse(pm$h8 > 75, "red","darkgreen"))

CEX may be related to how big the points will be.

References:
http://dapengde.com/archives/14824

http://dapengde.com/archives/14833

http://dapengde.com/archives/14836

http://dapengde.com/archives/14843
分享到:
评论

相关推荐

    rlang, 面向 R 编程的低层 API.zip

    rlang, 面向 R 编程的低层 API rlang 重要的: rlang API仍在成熟。 请参见 ?rlang::lifecycle 以获得被认为是稳定的函数列表。概述rlang软件包提供了使用 R 和tidyverse的核心语言功能的工具:系统

    javaee笔试题-tblog:技术博客站点

    rpy2 2017-12-21 rlang; 电阻 2018-02-16 新维; linux 2018-02-03 jupyter; 工作室; 笔记本; 系统 2018-03-08 护目镜; 乌本图 2018-03-19 Python; R; 包裹; 备份; 恢复; 点子; conda 2018-03-19 conda; 点子 ...

    R语言画图所需的各类型包

    都是我在画图中需要用到的包,如有补充欢迎:ggplot2_3.0.0/baidumap-master/bitops_1.0-6/colorspace_1.3-2/crayon_1.3.4/curl_3.2/devtools_1.13.6/digest_0.6.17/gtable_0.2.0/httr_1.3.1/lazyeval_0.2.1/mapdata...

    rlang:使用R进行编程的低级API

    rlang:使用R进行编程的低级API

    R语言程序包

    由于R软件版本问题,一些函数不能运用,在此编写了一个程序包可以使那些函数可以用。

    R-for-Data-Science-Lunchbreak-Lessons:LinkedIn学习课程的源文件

    R语言迷你教程 编程是一点点学习的。 您建立在基本概念上。 您将已经拥有的知识转移到另一种语言。 午餐休息课程在短期课程中讲授R(一种用于数据分析和报告的最受欢迎的编程语言),以扩展现有程序员已经知道的知识...

    ERROR: Package ‘gunicorn’ requires a different Python: 2.7.5 not in ‘>=3.4’

    Downloading http://pypi.doubanio.com/packages/69/ca/926f7cd3a2014b16870086b2d0fdc84a9e49473c68a8dff8b57f7c156f43/gunicorn-20.0.4-py2.py3-none-any.whl (77kB) |████████████████████...

    三层DLL架构解决方案-示范

    三层DLL架构解决方案-示范,值得研究一下

    Rcpp_0.12.2

    R是用于数据分析的语言,通过Rcpp可以将cpp库引入R中使用,适用于提高性能和接入原有算法库的场合。 Rcpp包提供C++类方便C or C++代码与R软件包进行交互,使用R中提供的.Call() 调用界面。 Rcpp提供R中的基础数据...

    erLang otp_win64_20.2安装包

    Erlang opt_win64 20.2 windows exe 安装包 Erlang 20.2 is the upcoming version of Erlang For Windows x64 installer 截至2018.01.25 groovy最新最稳定版本

    Ritmic:肿瘤微环境组成调节包装

    安装要从github获取当前的开发版本: git clone https://github.com/bcm-uga/RiTMIC.gitcd RiTMIC R构建包install.packages( " devtools " )# mixtools htmltools scales yaml lazyeval plyr rlang ggplot2 gtools ...

    RiTMIC:调节肿瘤微环境组成的包装

    # mixtools htmltools scales yaml lazyeval plyr rlang ggplot2 gtools caTools KernSmooth penda progress devtools :: load_all(); devtools :: document(); devtools :: install() 建立小插图 setwd("vignettes...

    Thinking in Erlang

    A GUIDE TO FUNCTIONAL PROGRAMMING IN E RLANG FOR THE EXPERIENCED PROCEDURAL DEVELOPER.

    dipsaus:R 包 dipsaus 的开发库

    数据分析和可视化的蘸酱 包装dipsaus提供add-ons到各种软件包,如shiny , rlang , future等,以增强这些软件包。 从Github安装开发版本包, devtools :: install_github( ' dipterix/dipsaus ' ) dipsaus从以下...

    by_row:一个替代rowwise()和mutate()+ map()的建议

    library( rlang ) 能够对数据帧执行按行操作很有用。 作为一个简单的示例(显然,在实践中,这些列不应该是列表列,但现在忽略它),这将失败: tibble( a = list ( 10 , 11 ), b = list ( 3 , 4 )) % &gt; % mutate...

    ecg:Erlang函数调用图生成器

    ECG是E rlang函数C的所有图G enerator,其自动绘制函数调用和处理程序派生。 ECG利用Erlang强大的跟踪机制和可视化工具graphviz ,确保已安装graphviz ,并且由于此处使用Python绑定,因此还需要安装Digraph软件包。...

    哑铃

    library(ggplot2) library(rlang) library(utils) &gt; a&lt;-read_tsv("./Tonsils_RNA_QC.txt") Parsed with column specification: cols( Group = col_character(), SampleID = col_character(), Subject = col_...

    两个相关系数的比较检验: corr_rtest(ra, rb, na, nb):两个相关系数的比较-matlab开发

    两个相关系数的比较[p, z, za, zb] = corr_rtest(ra, rb, na, nb) 灵感来自 Rlang 的 r.test() http://personality-project.org/r/html/r.test.html

Global site tag (gtag.js) - Google Analytics