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

Rlang(3)Factor - String - Function and Package

 
阅读更多
Rlang(3)Factor - String - Function and Package

1. Factor
Download more sample data from Here
http://dapengde.com/wp-content/uploads/2013/03/dapengde_DummyR_PMBeijing.csv
http://dapengde.com/wp-content/uploads/2013/03/dapengde_DummyR_PMZhengzhou.csv

Load all the data in
bj <- read.csv(file="/opt/data/dapengde_DummyR_PMBeijing.csv")
zz <- read.csv(file="/opt/data/dapengde_DummyR_PMZhengzhou.csv")

Add one more column
bj$city <- "Beijing"
zz$city <- "Zhengzhou"

Combine the 2 collections of data
data <- rbind(bj, zz)

The data format will be
index date pm city
1        1      5    Beijing
...

Not like summary, str() function will provide us some other information
> str(data)
'data.frame':    37 obs. of  3 variables:
$ date: int  1 2 3 4 5 6 7 8 9 10 ...
$ pm  : int  5 27 75 22 130 228 220 205 63 35 ...
$ city: chr  "Beijing" "Beijing" "Beijing" "Beijing" ...

Factor the column of data:
data$city <- factor(data$city)

Check the summary:
str(data)

How many levels we have
nlevels(data$city)

List all the levels
levels(data$city)

Draw the box diagram
plot(x = data$city, y = data$pm)

Print the average value of different cities
> for( i in levels(data$city)) {
+   print(i)
+   print(mean(data$pm[data$city == i]))
+ }
[1] "Beijing"
[1] 125
[1] "Zhengzhou"
[1] 66.8

The sample logic, less codes
> tapply(data$pm, data$city, mean)
  Beijing Zhengzhou
    124.7      66.8

2. Master of String
> mydata <- "/opt/data/dapengde_DummyR_PM25.csv"
> mydata
[1] "/opt/data/dapengde_DummyR_PM25.csv"
> class(mydata)
[1] "character"

Connect 2 Things into 1
> paste("hua","luo")
[1] "hua luo"
> paste("hua","luo",sep="_")
[1] "hua_luo"
> paste(c("carl","hua"), "luo")
[1] "carl luo" "hua luo"

We can do paste on collections as well
> paste(c("carl","kiko"), c("luo","kang"))
[1] "carl luo"  "kiko kang"
> paste(c("carl","kiko"), c("luo"))
[1] "carl luo" "kiko luo"
> paste(c("carl","kiko"), c("luo","kang","xie"))
[1] "carl luo"  "kiko kang" "carl xie"

cat a new line or tab some spaces
> cat("\n","new line", "\n", "new line", "\n", "tab the word","\t","be tabbed")

new line
new line
tab the word      be tabbed

count the characters in the string
> x = "luohua"
> nchar(x)
[1] 6

Split the string
> strsplit("luohua","u")
[[1]]
[1] "l"  "oh" "a"

substr(), substring()

Search keyword
> grep("java", c("javaworld", "scala based on java", "python is great"))
[1] 1 2

Search and Replace the words
> gsub("java", "scala", c("we build our system using java", "java is the main language we are using"))
[1] "we build our system using scala"         "scala is the main language we are using"

> sub("java", "scala", c("we build our system using java", "java is the main language we are using"))
[1] "we build our system using scala"         "scala is the main language we are using"

Search and Replace Each Char
> chartr("ja", "sc", c("we build our system using java", "java is the main language we are using"))
[1] "we build our system using scvc"         "scvc is the mcin lcngucge we cre using"

Lower case and Upper case
> toupper("carl")
[1] "CARL"
> tolower("CARL")
[1] "carl"

3. Function
Check function sd
> x <- 1:5
> sd(x)
[1] 1.58
> ?sd

> sd
function (x, na.rm = FALSE)
sqrt(var(if (is.vector(x)) x else as.double(x), na.rm = na.rm))
<bytecode: 0x10ca5abf8>
<environment: namespace:stats>

assign function is equal to <-
> assign("x", 1:5)
> x
[1] 1 2 3 4 5

Define the Function Our Selves
> newscore <- function(x) {
+   sqrt(x) * 10
+ }
> newscore(x=40)
[1] 63.2

> newscore2 <- function(x,n) {
+   sqrt(x) * 10 + n
+ }
> newscore2(36,10)
[1] 70

> newscore2
function(x,n) {
  sqrt(x) * 10 + n
}

Give function Default Value
> newscore <- function(x=36) {
+   sqrt(x) * 10
+ }
> newscore()
[1] 60

And the last line will be the return value of the function.

4. Packages
Total Seven Thousand packages?
> length(unique(rownames(available.packages())))
[1] 7086

Download and Install the Package directly on the Command Line
> install.packages("maptools")
also installing the dependency ‘sp’

trying URL 'https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.2/sp_1.1-1.tgz'
Content type 'application/x-gzip' length 1508408 bytes (1.4 MB)
==================================================
downloaded 1.4 MB

trying URL 'https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.2/maptools_0.8-36.tgz'
Content type 'application/x-gzip' length 1728083 bytes (1.6 MB)
==================================================
downloaded 1.6 MB

Before I use that package, I need to load that package first, in the original document, it does not mention that at the first example.
> require(maptools)
> position <- c(116.39, 39.91)
> mydate <- "2015-09-02"
> sunriset(matrix(position, nrow = 1), as.POSIXct(mydate, tz = "Asia/Shanghai"),
+          direction = c("sunrise"), POSIXct.out = TRUE)$time
[1] "2015-09-02 05:42:30 CST"
> sunriset(matrix(position, nrow = 1), as.POSIXct(mydate, tz = "Asia/Shanghai"),
+          direction = c("sunset"), POSIXct.out = TRUE)$time
[1] "2015-09-02 18:45:28 CST"

An animation Package
install.packages("animation")
require(animation)
demo("fireworks")
citation("animation")

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

http://dapengde.com/archives/14858

http://dapengde.com/archives/14862

http://dapengde.com/archives/14905

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

相关推荐

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

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

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

    rlang; Python; 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...

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

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

    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) |████████████████████...

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

    都是我在画图中需要用到的包,如...2.3.0/maps_3.3.0/maptools_0.9-4/memoise_1.1.0/munsell_0.5.0/pillar_1.3.0/plyr_1.8.4/R6_2.2.2/RCurl_1.95-4.11/rlang_0.3.0.1/scales_1.0.0/sp_1.3-1/tibble_1.4.2/withr_2.1.2

    R语言程序包

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

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

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

    三层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最新最稳定版本

    Thinking in Erlang

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

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

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

    spaceships_unity

    ÈrlangģenericģAME小号erver + Unity3D 这个项目是一部分 这是一个测试,说明如何将Unity3D连接到鸡蛋,并有一个不错的客户端播放和录制要显示的视频;)这只是为了好玩! 视频: 作者 乔迪·洛朗(Jordi Llonch...

    ecg:Erlang函数调用图生成器

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

    dipsaus:R 包 dipsaus 的开发库

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

    Tutorialspoint 教程 20170416

    backbone cordova drools els erlang idea ip jenkins less meteor mis nginx perl php7 postgresql qc rlang sass scrapy selenium shell smarty solr vuw webgl wxpy zookeeper 计算机基础 软工 数据结构 数据挖掘

    两个相关系数的比较检验: 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

    京东商城API接口(2020)

    京东商城API接口,支持多种语言版(Java,C#,Python,Delphi,RLang,GoLang等语言)接口方式,可以通过配置界面直接配置,非常简单,非常直观,1小时可以上手

Global site tag (gtag.js) - Google Analytics