1. 程式人生 > >R語言:文字(字串)處理與正則表示式

R語言:文字(字串)處理與正則表示式

這兩個函式返回向量水平的匹配結果,不涉及匹配字串的詳細位置資訊。

grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes =FALSE, invert = FALSEgrepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

雖然引數看起差不多,但是返回的結果不一樣。下來例子列出C:\windows目錄下的所有檔案,然後用grep和grepl查詢exe檔案:

files <- list.files("c:/windows"grep("\\.exe$", files)
## [1] 8 28 30 35 36 58 69 99 100 102 111 112 115 117
grepl("\\.exe$", files)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE 
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
## [23] FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE 
## [34] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
## [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
## [56] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 ## [67] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 ## [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
## [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
 ## [100] TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
## [111] TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE

grep僅返回匹配項的下標,而grepl返回所有的查詢結果,並用邏輯向量表示有沒有找到匹配。兩者的結果用於提取資料子集的結果都一樣:

files[grep("\\.exe$", files)]
## [1] "bfsvc.exe" "explorer.exe" "fveupdate.exe" "HelpPane.exe" ## [5] "hh.exe" "notepad.exe" "regedit.exe" "twunk_16.exe" ## [9] "twunk_32.exe" "uninst.exe" "winhelp.exe" "winhlp32.exe" ## [13] "write.exe" "xinstaller.exe"
files[grepl("\\.exe$", files)]
## [1] "bfsvc.exe" "explorer.exe" "fveupdate.exe" "HelpPane.exe" ## [5] "hh.exe" "notepad.exe" "regedit.exe" "twunk_16.exe" ## [9] "twunk_32.exe" "uninst.exe" "winhelp.exe" "winhlp32.exe" ## [13] "write.exe" "xinstaller.exe"

相關推薦

R語言文字字串處理表示式

這兩個函式返回向量水平的匹配結果,不涉及匹配字串的詳細位置資訊。 grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes =FALSE, invert = FALSE) grepl

R語言文字字串處理表示式

處理文字是每一種計算機語言都應該具備的功能,但不是每一種語言都側重於處理文字。R語言是統計的語言,處理文字不是它的強項,perl語言這方面的功能比R不知要強多少倍。幸運的是R語言的可擴充套件能力很強,DNA/RNA/AA等生物序列現在已經可以使用R來處理。nchar字元的個數

Groovy入門-字串處理表示式

字串處理-1 println ‘lxt008 said "Groovy"' println "lxt008 said 'Grails'" def str1 = 'Groovy&Grails&lxt008' println str1[4]

爬蟲筆記——如何寫表示式詳解

什麼是正則表示式?   正則表示式(Regular Expression)是一種文字模式,在編寫處理字串的程式或網頁時,經常會有查詢符合某些規則的字串的需求。正則表示式就是用於描述這些規則的工具,換句話說,正則表示式就是記錄文字規則的程式碼。我們將分別從原子、元子符、模式修

R語言學習筆記十三時間序列

abs 以及 stat max 時間 aic air ror imp #生成時間序列對象 sales<-c(18,33,41,7,34,35,24,25,24,21,25,20,22,31,40,29,25,21,22,54,31,25,26,35) tsal

R語言學習筆記資料處理

本文的示例資料框集(egData)如下: 值標籤: if(FALSE){值標籤,levels代表變數實際值,labels代表標籤值} egData$sex <- factor(egDa

R語言bootstrap分析boot

//## bootstrap分析資料,package = "boot" > library(boot) > city u x 1 138 143 2 93 104 3 61 69 4 179 260 5 48 75 6 37 63 7

R語言-線圖

  1.線圖示例 plot()為高水平作圖命令,axis()、lines()、legend()都為低水平作圖命令 > rain<-read.csv("cityrain.csv") > plot(rain$Tokyo,type="b",lwd=2,

R語言初學指南筆記

R語言的下載地址(Windows版本):http://ftp.ctex.org/mirrors/CRAN/ R語言的安裝,需要額外安裝mikTex,Rtools,htmlhelp: R:D:\Program Files\R\R-3.0.3 mikTex:D:\Program Files\Mi

R語言——學習筆記

1.“>”符號後輸入指令。 2.1:100   指輸入1—100連續數值 3.sum()    函式,計算總和 4.sample()    函式,隨機取出     例1:>sample(1:6,1)    從1—6的整數中取出1個     例2:&g

R語言——學習筆記

1.用R語言匯入Excel檔案中“xlsx”和“xls”格式的資料。 (1)安裝“readxl”包:     >install.packages("readxl") (2)將Excel資料匯入R中,檔案位於“C:\Users\Celin\Desktop”路徑的

R語言——學習筆記

1.table()    函式,生成表 2.管道處理    %>%    管道運算子     >install.packages("dplyr")     >library(dplyr)     >dat %>% table    #用左

c++文字字串格式轉換

//QString ->const wchar_t * const wchar_t * fileNameC = reinterpret_cast<const wchar_t *>([QString].utf16()); //const wchar_t *

python基礎資料型別str字串__003

1、str(字串):python3中凡是引號括起來的都是字串(包括單引號'字串內容',雙引號"字串內容") 2、字串的索引和切片 (1)索引 s = 'sdflajdf1212' print(s[0]) #擷取字串s的第一個元素s print(s[-1]) #擷取字串s的最後一個元

2-2-3序列字串乘法p32

以正確的寬度在劇中的“盒子”內列印一個句子 分析: 定義螢幕寬度為80,輸入的句子長度為26,盒子寬度=句子長度+6=26+6=32; 左邊距=右邊距,左邊距+右邊距+盒子寬度=螢幕寬度; 左邊距=(螢幕寬度-盒子寬度)/2=(80-32)/2=24; 格式輸出: 盒子四個角為+號,左面是空格,右

R語言入門——筆記建立資料集

第一部分:資料結構 1.建立向量:c() c(…, recursive = FALSE, use.names = TRUE) 根據元素座標訪問 2.建立矩陣:matrix() matrix(data = NA, nrow = 1, ncol

R語言--資料介面

1. CSV檔案 獲取和設定工作目錄 # 獲取和設定工作目錄 print(getwd()) # 設定當前工作目錄 setwd("E:/R") print(getwd()) 列印結果:

python 全棧開發str字串常用方法操作 、for 有限迴圈以及if 迴圈

  str(字串)常用方法操作: 首字母大寫: s = 'mylovepython' s1 = s.capitalize() print(s1) 輸出: Mylovepython 單行多字串首字母大寫: s = 'my love python' s2 = s.title(

Python 全棧開發str字串索引和切片

str(字串)索引和切片 str(字串)索引: #計算機中大部分索引以0為開始 s = 'mylovepython' s1 = s[0] s2 = s[4] s3 = s[-1] print(s1,'mylovepython中索引為0的字串') print(s2,'mylovepython中索引為4

資料分析--用R語言預測離職

資料分析–用R語言預測離職(下) 接上一篇~ 接下來我們探索離職和其他分類變數的關係~ > library(scales) > k1 <- ggplot(attr.df, aes(x=Gender,fill=Attrition))+