R语言函数化学习笔记

1.apply函数

可以让list或者vector的元素依次执行一遍调用的函数,输出的结果是list格式

2.sapply函数

原理和list一样,但是输出的结果是一个向量的形式

3.vapply

这个函数输出的结果更加详细,但是函数使用的时候需要多写一个几个参数来控制

Use vapply

Before you get your hands dirty with the third and last apply function that you'll learn about in this intermediate R course, let's take a look at its syntax. The function is called vapply(), and it has the following syntax:

  1. vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

Over the elements inside X, the function FUN is applied. The FUN.VALUE argument expects a template for the return argument of this function FUN. USE.NAMES is TRUE by default; in this case vapply() tries to generate a named array, if possible.

  1. temp is already prepared for you in the workspace
  2. Definition of below_zero()
  3. below_zero <- function(x) {
  4. return(x[x < 0])
  5. }
  6. # Apply below_zero over temp using sapply(): freezing_s
  7. freezing_s<-sapply(temp,below_zero)
  8. # Apply below_zero over temp using lapply(): freezing_l
  9. freezing_l<-lapply(temp,below_zero)
  10. # Are freezing_s and freezing_l identical?
  11. dentical(freezing_s,freezing_l)

举个例子就知道结果的区别了

temp is already prepared for you in the workspace

Definition of below_zero()

below_zero <- function(x) {

return(x[x < 0])

}

Apply below_zero over temp using sapply(): freezing_s

freezing_s<-sapply(temp,below_zero)

Apply below_zero over temp using lapply(): freezing_l

freezing_l<-lapply(temp,below_zero)

Are freezing_s and freezing_l identical?

identical(freezing_s,freezing_l)

可以看一下改写

  1. # temp is already defined in the workspace
  2. # Convert to vapply() expression
  3. sapply(temp, max)
  4. vapply(temp,max,numeric(1))
  5. # Convert to vapply() expression
  6. sapply(temp, function(x, y) { mean(x) > y }, y = 5)
  7. vapply(temp,function(x, y){ mean(x) > y },logical(1), y = 5)

除了以上的函数之外,还有

tapply

mapply

。。。

以下是常用的一些简单函数

seq():

Generate sequences, by specifying the from, to, and by arguments.

rep():

Replicate elements of vectors and lists.

sort():

Sort a vector in ascending order. Works on numerics, but also on character strings and logicals.

rev():

Reverse the elements in a data structures for which reversal is defined.

str():

Display the structure of any R object.

append():

Merge vectors or lists.

is.*():

Check for the class of an R object.

as.*():

Convert an R object from one class to another.

unlist():

Flatten (possibly embedded) lists to produce a vector.

  1. # Fix me
  2. > rep(seq(1, 7, by = 2), times = 7)
  3. [1] 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7
  4. > rep(seq(1, 7, by = 2), each = 7)
  5. [1] 1 1 1 1 1 1 1 3 3 3 3 3 3 3 5 5 5 5 5 5 5 7 7 7 7 7 7 7
  6. > a<-rep(2008:2018, times = 11)
  7. > a
  8. [1] 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2008 2009 2010 2011
  9. [16] 2012 2013 2014 2015 2016 2017 2018 2008 2009 2010 2011 2012 2013 2014 2015
  10. [31] 2016 2017 2018 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2008
  11. [46] 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2008 2009 2010 2011 2012
  12. [61] 2013 2014 2015 2016 2017 2018 2008 2009 2010 2011 2012 2013 2014 2015 2016
  13. [76] 2017 2018 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2008 2009
  14. [91] 2010 2011 2012 2013 2014 2015 2016 2017 2018 2008 2009 2010 2011 2012 2013
  15. [106] 2014 2015 2016 2017 2018 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
  16. [121] 2018
  17. >
  1. > # Create first sequence: seq1
  2. > seq1<-seq(1,500,by=3)
  3. >
  4. > # Create second sequence: seq2
  5. > seq2<-seq(1200,900,by=-7)
  6. >
  7. > # Calculate total sum of the sequences
  8. > sum(seq1,seq2)
  9. [1] 87029

grepl & grep

这两个函数其实挺常用的就是match

In their most basic form, regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings. For this purpose, you can use:

grepl(), which returns TRUE when a pattern is found in the corresponding character string.

grep(), which returns a vector of indices of the character strings that contains the pattern.

Both functions need a pattern and an x argument, where pattern is the regular expression you want to match for, and the x argument is the character vector from which matches should be sought.

  1. grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
  2. fixed = FALSE, useBytes = FALSE, invert = FALSE)
  3. grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
  4. fixed = FALSE, useBytes = FALSE)
  5. sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
  6. fixed = FALSE, useBytes = FALSE)
  7. gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
  8. fixed = FALSE, useBytes = FALSE)
  9. regexpr(pattern, text, ignore.case = FALSE, perl = FALSE,
  10. fixed = FALSE, useBytes = FALSE)
  11. gregexpr(pattern, text, ignore.case = FALSE, perl = FALSE,
  12. fixed = FALSE, useBytes = FALSE)
  13. regexec(pattern, text, ignore.case = FALSE, perl = FALSE,
  14. fixed = FALSE, useBytes = FALSE)

demo

  1. > # The emails vector has already been defined for you
  2. > emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
  3. "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
  4. >
  5. > # Use grepl() to match for "edu"
  6. >
  7. > grepl(pattern="edu",emails)
  8. [1] TRUE TRUE FALSE TRUE TRUE FALSE
  9. > # Use grep() to match for "edu", save result to hits
  10. > hits<-grep(pattern="edu",emails)
  11. >
  12. > # Subset emails using hits
  13. > emails[hits]
  14. [1] "john.doe@ivyleague.edu" "education@world.gov"
  15. [3] "invalid.edu" "quant@bigdatacollege.edu"

You can use the caret, ^, and the dollar sign, $ to match the content located in the start and end of a string, respectively. This could take us one step closer to a correct pattern for matching only the ".edu" email addresses from our list of emails. But there's more that can be added to make the pattern more robust:

@, because a valid email must contain an at-sign.

., which matches any character (.) zero or more times (). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the ".edu" portion of an email address.

\.edu$, to match the ".edu" part of the email at the end of the string. The \ part escapes the dot: it tells R that you want to use the . as an actual character.

对于一些特殊的符号,需要使用\来进行转码,否者会被识别错误

  1. > # The emails vector has already been defined for you
  2. > emails <- c("john.doe@ivyleague.edu", "education@world.gov", "dalai.lama@peace.org",
  3. "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
  4. >
  5. > # Use grepl() to match for .edu addresses more robustly
  6. > grepl(pattern="@.*\\.edu$",emails)
  7. [1] TRUE FALSE FALSE FALSE TRUE FALSE
  8. >
  9. > # Use grep() to match for .edu addresses more robustly, save result to hits
  10. > hits<-grep(pattern="@.*\\.edu$",emails)
  11. >
  12. >
  13. > # Subset emails using hits
  14. > emails[hits]
  15. [1] "john.doe@ivyleague.edu" "quant@bigdatacollege.edu"

sub & gsub

While grep() and grepl() were used to simply check whether a regular expression could be matched with a character vector, sub() and gsub() take it one step further: you can specify a replacement argument. If inside the character vector x, the regular expression pattern is found, the matching element(s) will be replaced with replacement.sub() only replaces the first match, whereas gsub() replaces all matches.

Suppose that emails vector you've been working with is an excerpt of DataCamp's email database. Why not offer the owners of the .edu email addresses a new email address on the datacamp.edu domain? This could be quite a powerful marketing stunt: Online education is taking over traditional learning institutions! Convert your email and be a part of the new generation!

这两个函数就是替换的意思

替换指定的字符

  1. > # The emails vector has already been defined for you
  2. > emails <- c("john.doe@ivyleague.edu", "education@world.gov", "global@peace.org",
  3. "invalid.edu", "quant@bigdatacollege.edu", "cookie.monster@sesame.tv")
  4. >
  5. > # Use sub() to convert the email domains to datacamp.edu
  6. > sub("@.*\\.edu$","@datacamp.edu",emails)
  7. [1] "john.doe@datacamp.edu" "education@world.gov"
  8. [3] "global@peace.org" "invalid.edu"
  9. [5] "quant@datacamp.edu" "cookie.monster@sesame.tv"

日期

  1. > # Get the current date: today
  2. > today <- Sys.Date()
  3. >
  4. > # See what today looks like under the hood
  5. > unclass(today)
  6. [1] 18238
  7. >
  8. > # Get the current time: now
  9. > now <- Sys.time()
  10. >
  11. > # See what now looks like under the hood
  12. > unclass(now)
  13. [1] 1575802501

常用的格式

Create and format dates

To create a Date object from a simple character string in R, you can use the as.Date() function. The character string has to obey a format that can be defined using a set of symbols (the examples correspond to 13 January, 1982):

%Y: 4-digit year (1982)

%y: 2-digit year (82)

%m: 2-digit month (01)

%d: 2-digit day of the month (13)

%A: weekday (Wednesday)

%a: abbreviated weekday (Wed)

%B: month (January)

%b: abbreviated month (Jan)

demo

  1. > # Definition of character strings representing dates
  2. > str1 <- "May 23, '96"
  3. > str2 <- "2012-03-15"
  4. > str3 <- "30/January/2006"
  5. >
  6. > # Convert the strings to dates: date1, date2, date3
  7. > date1 <- as.Date(str1, format = "%b %d, '%y")
  8. > date2 <- as.Date(str2)
  9. > date3 <- as.Date(str3, format = "%d/%B/%Y")
  10. >
  11. > # Convert dates to formatted strings
  12. > format(date1, "%A")
  13. [1] "Thursday"
  14. > format(date2, "%d")
  15. [1] "15"
  16. > format(date3, "%b %Y")
  17. [1] "Jan 2006"

R语言函数化学习笔记6的更多相关文章

  1. R语言函数化学习笔记3

    R语言函数化学习笔记3 R语言常用的一些命令函数 1.getwd()查看当前R的工作目录 2.setwd()修改当前工作目录 3.str()可以输出指定对象的结构(类型,位置等),同理还有class( ...

  2. R语言函数化学习笔记4

    条件语句和循环语句 当你说话时候用到了如果,此时条件出现了 举个条件函数的例子 sign_t<-function(x){ if(x>0){ return(1) }else if(x< ...

  3. R语言函数化编程笔记2

    R语言函数化编程笔记2 我学过很多的编程语言,可以我写的代码很啰嗦,一定是我太懒了.或许是基础不牢地动山摇 1.为什么要学函数 函数可以简化编程语言,减少重复代码或者说面向对象的作用 2.函数 2.1 ...

  4. R语言函数化编程笔记1

    R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...

  5. R语言函数话学习笔记5

    使用Tidyverse完成函数化编程 (参考了家翔学长的笔记) 1.magrittr包的使用 里面有很多的管道函数,,可以减少代码开发时间,提高代码可读性和维护性 1.1 四种pipeline 1.1 ...

  6. R语言与机器学习学习笔记

    人工神经网络(ANN),简称神经网络,是一种模仿生物神经网络的结构和功能的数学模型或计算模型.神经网络由大量的人工神经元联结进行计算.大多数情况下人工神经网络能在外界信息的基础上改变内部结构,是一种自 ...

  7. R语言与显著性检验学习笔记

    R语言与显著性检验学习笔记 一.何为显著性检验 显著性检验的思想十分的简单,就是认为小概率事件不可能发生.虽然概率论中我们一直强调小概率事件必然发生,但显著性检验还是相信了小概率事件在我做的这一次检验 ...

  8. 【数据分析 R语言实战】学习笔记 第十一章 对应分析

    11.2对应分析 在很多情况下,我们所关心的不仅仅是行或列变量本身,而是行变量和列变量的相互关系,这就是因子分析等方法无法解释的了.1970年法国统计学家J.P.Benzenci提出对应分析,也称关联 ...

  9. 【数据分析 R语言实战】学习笔记 第四章 数据的图形描述

    4.1 R绘图概述 以下两个函数,可以分别展示二维,三维图形的示例: >demo(graphics) >demo(persp) R提供了多种绘图相关的命令,可分成三类: 高级绘图命令:在图 ...

随机推荐

  1. C++ 常用编程--Swap函数有几种写法?

    C++ 常用编程--Swap函数有几种写法? 在说C++模板的方法前,我们先想想C语言里面是怎么做交换的. 举个例子,要将两个int数值交换,是不是想到下面的代码: void swap(int& ...

  2. 【渗透实战】web渗透实战,手动拿学校站点 得到上万人的信息(漏洞已提交)

    ------------恢复内容开始------------ ’‘’版权tanee转发交流学校请备注漏洞已经提交学校管理员关键过程的截图和脚本代码已经略去.希望大家学习技术和思路就好,切勿进行违法犯罪 ...

  3. Vue中的递归组件

    递归函数我们都再熟悉不过了,也就是函数自己调用自己.递归组件也是类似的,在组件的template内部使用组件自身.那递归组件有什么使用场景呢? 我们都知道树这个数据结构就是一种递归的结构,因此我们可以 ...

  4. 「Flink」使用Java lambda表达式实现Flink WordCount

    本篇我们将使用Java语言来实现Flink的单词统计. 代码开发 环境准备 导入Flink 1.9 pom依赖 <dependencies> <dependency> < ...

  5. linux 基础入门(9) 系统服务 systemctl 与 xinted的运用

    9.系统服务 9.1系统服务 可以把计算机理解为一个地点比如中关村大街系统服务理解为中关村大街的理发店.饭店.商场等等,每一个都是一个系统服务,为客户提供不同内容的服务 服务:常驻在内存中的程序,且可 ...

  6. P5727 【深基5.例3】冰雹猜想

    链接:Miku -------------------- 欸,为什么我第一遍没过 -------------------- #include<iostream> using namespa ...

  7. 硬盘500M,为什么没有500M。10M宽带,为什么网速没有10M?

    在天朝, 硬件厂商用1000代替1024, 通信公司,用 byte来代替bit. 比如 500G的硬盘,应该有 500 * 1024 *1024 *8 = 4.194304*10^9 位 但是按照厂商 ...

  8. mac 15 IDA7.0 下载安装

    吾爱破解上有相应的解决办法,在低版本mac上安装完成后,直接拖到15版本,再打上补丁,补丁可以自己去找,下面是转好了的,mac解压最好不要用自带的解压软件,用BetterZip试试,不行就多解压几次, ...

  9. jQuery XSS漏洞

    漏洞成因: jQuery中过滤用户输入数据所使用的正则表达式存在缺陷,可能导致location.hash跨站脚本攻击. 演示程序: <!DOCTYPE html> <html lan ...

  10. SQL Server无备份误删数据的恢复

    在正式生产数据库中,因为客户现场管理不规范产生了一条错误数据,由于自身睡眠不佳加上客户方言表达,将编号记错,在没有备份的情况下,直接连远程数据库执行了delete操作. 由于备份设置的是每日0点,当天 ...