R语言学习之数据的清理和转化
2015-11-04 20:10
260 查看
处理字符串
grep grepl 和regexpr函数都能找到与模式相匹配的字符串 sub 和 gsub函数能替换匹配的字符串
加载strngr包,fixed里面为要匹配的字符串 返回匹配的字符串序列
使用正则表达式来匹配多个要匹配的字符串,这是来匹配逗号和and
st_count是用来统计有多少个字符串
str_replace函数来代替字符串中的某一个
ignore.case来忽略某一个字符或字符串
grep grepl 和regexpr函数都能找到与模式相匹配的字符串 sub 和 gsub函数能替换匹配的字符串
加载strngr包,fixed里面为要匹配的字符串 返回匹配的字符串序列
> library(stringr) > multiple <- str_detect(english_monarchs$domain,fixed(",")) > english_monarchs[multiple,c("name","domain")] name domain 17 Offa East Anglia, Mercia 18 Offa East Anglia, Kent, Mercia 19 Offa and Ecgfrith East Anglia, Kent, Mercia 20 Ecgfrith East Anglia, Kent, Mercia 22 C<U+009C>nwulf East Anglia, Kent, Mercia 23 C<U+009C>nwulf and Cynehelm East Anglia, Kent, Mercia 24 C<U+009C>nwulf East Anglia, Kent, Mercia 25 Ceolwulf East Anglia, Kent, Mercia 26 Beornwulf East Anglia, Mercia 82 Ecgbehrt and <U+00C6>thelwulf Kent, Wessex 83 Ecgbehrt and <U+00C6>thelwulf Kent, Mercia, Wessex 84 Ecgbehrt and <U+00C6>thelwulf Kent, Wessex 85 <U+00C6>thelwulf and <U+00C6>eelstan I Kent, Wessex 86 <U+00C6>thelwulf Kent, Wessex 87 <U+00C6>thelwulf and <U+00C6>eelberht III Kent, Wessex 88 <U+00C6>eelberht III Kent, Wessex 89 <U+00C6>thelred I Kent, Wessex 95 Oswiu Mercia, Northumbria
使用正则表达式来匹配多个要匹配的字符串,这是来匹配逗号和and
> ruler <- str_detect(english_monarchs$name,",|and") > english_monarchs[ruler & !is.na(ruler)]把name一列拆分掉,则可以使用str_splist函数
> indival <- str_split(english_monarchs$name,",|and") > head(indival[sapply(indival,length)>1]) [[1]] [1] "Sigeberht " " Ecgric" [[2]] [1] "Hun" " Beonna " " Alberht" [[3]] [1] "Offa " " Ecgfrith" [[4]] [1] "C\u009cnwulf " " Cynehelm" [[5]] [1] "Sighere " " Sebbi" [[6]] [1] "Sigeheard " " Swaefred"
st_count是用来统计有多少个字符串
> str_count(english_monarchs$name,th)
str_replace函数来代替字符串中的某一个
ignore.case来忽略某一个字符或字符串
相关文章推荐
- Apriori algorithm---数据挖掘初学1
- maven之Nexus的配置【setting.xml里配置<profile>】(六)
- infobright源码安装
- 用JDK中的 jps、jinfo、jstat、jstack、jmap、jconsole等命令对JVM,内存,线程进行分析和故障诊断
- MFC函数启动执行过程
- 1104 冒泡排序语法树
- php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)
- 设计模式六大原则(3)-依赖倒置原则
- Metal Framework基础使用教程
- 百度地图(基本定位和地理编码)
- 贝叶斯决策
- python json path xpath
- delphi richedit TopIndex
- Ubuntu下安装mysql和使用
- 阅读verilog程序总结
- 虚幻引擎 4.9
- 三个球的旋转的动画
- hdu1005 Number Sequence
- 【JAVA大作业开发记录(一)】
- hdu5521Meeting(dijkstra最短路)icpc沈阳赛区M题