美文网首页生信星球培训第五期
第10章 使用stringr处理字符串 P1

第10章 使用stringr处理字符串 P1

作者: 李其龙_ec81 | 来源:发表于2018-10-17 16:47 被阅读1次

    第10章 使用stringr处理字符串

    这一章学之前感觉真的难,学之后感觉确实难 :)

    其实本章的重点正如书中所示在于使用正则表达式处理字符型数据,首先要学会正则表达式啊!这个名字听起来就是很厉害的样子...原谅我小白的无限崇拜

    准备工作

    library(tidverse)
    library(stringr)
    
    

    首先创建一个字符串

    (test_stringr1<- "hellow word")
    #记得加()是既加入变量又打印吧 
    
    class(test_stringr1)
    [1] "character"
    

    这里书上先介绍了为什么“”或者’‘在里面不能算是一个字符型的向量,也介绍了如果想表示应该如何进行表达。其实这里面的转义运用的就是后边要介绍的正则表达式。所以读到这里很懵b的状态也没关系。

    计算字符串的长度

    这里面使用了stringi函数,先看看stringi函数里面究竟有什么吧!(提示使用help()函数)

    stringi is THE R package for fast, correct, consistent, and convenient string/text manipulation. It gives predictable results on every platform, in each locale, and under any “native” character encoding.

    Keywords: R, text processing, character strings, internationalization, localization, ICU, ICU4C, i18n, l10n, Unicode.

    str_length(c(test_stringr1,NA))
    [1] 11 NA
    

    A numeric vector giving number of characters (code points) in each element of the character vector. Missing string have missing length.
    很明显str_length函数的默认值是不计算缺失值NA的

    字符串的组合

    这里主要介绍了str_c函数,这个函数就是连接前后两个或多个字符型变量,连接符号(seq)可以自定义。这里比较有意思的是向量化这个,感觉说向量化感觉很吊,我个人感觉就是一个的去匹配几个的字符型变量。有点像数学公式x(x1+x2+x3)=xx1+xx2+xx3

    关于if那个本书写的有点省略,我补全一下

    name <- "Hadley"
    time_of_day <- "morning"
    birthday <- FALSE
    str_c("Good",time_of_day," ",name,
    +       if(birthday==FALSE)" and HAPPLY BIRTHDAT",".")
    "Goodmorning Hadley and HAPPLY BIRTHDAT."

    相关文章

      网友评论

        本文标题:第10章 使用stringr处理字符串 P1

        本文链接:https://www.haomeiwen.com/subject/cryrzftx.html