R中grep()与grepl()之间的区别
在本文中,我们将讨论 R 编程语言中 grep() 和 grepl() 之间的区别。
grep()
R 语言中的这个grep()函数允许程序员在给定的字符串集合中搜索特定模式的匹配项。语法如下,
Syntax: grep(stringPattern, x, ignore.case=TRUE or FALSE, value=TRUE or FALSE)
Parameters:
- stringPattern: A pattern that has to be matched with given elements of the string.
- x: Specified string vector.
- ignore.case: If its value is TRUE, it ignores case.
- value: If its value is TRUE, it return the matching elements vector, else return the indices vector.
示例 1:
R
# R program to illustrate
# grep function
# Initializing a string vector
x <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grep() function
grep("GeeksforGeeks", x)
grep("Bhuwanesh", x)
grep("gfg", x, ignore.case = FALSE)
grep("Nainwal", x, ignore.case = TRUE)
R
# R program to illustrate
# grep function
# Creating string vector
x <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grep() function
grep("gfg", x, ignore.case = TRUE, value = TRUE)
grep("Bhuwanesh", x, ignore.case = TRUE, value = TRUE)
grep("GeeksforGeeks", x, ignore.case = FALSE, value = FALSE)
grep("Nainwal", x, ignore.case = FALSE, value = FALSE)
R
# R program to illustrate
# grepl function
# Initializing a string vector
str <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grepl() function
grepl("GeeksforGeeks", str)
grepl("Bhuwanesh", str)
grepl("gfg", str)
grepl("Nainwal", str)
R
# R program to illustrate
# grepl function
# Creating string vector
x <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grepl() function
grepl("gfg", x, ignore.case = TRUE)
grepl("Bhuwanesh", x, ignore.case = TRUE)
grepl("GeeksforGeeks", x, ignore.case = TRUE)
grepl("Nainwal", x, ignore.case = TRUE)
R
# create a vector of data
data <- c("GeeksforGeeks", "gfg", "Bhuwanesh",
"Nainwal", "Swift")
grep("GeeksforGeeks", data)
grepl("Bhuwanesh", data)
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT',
'ECE', 'EE',
'ME'),
Strength = c(80, 76, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# select columns that contain the string
# 'S' in their name
df %>% select(grep('S', colnames(df)))
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT', 'ECE',
'EE', 'ME'),
Strength = c(80, 76, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# select and count columns that contain
# the string 'S' in their name
df %>% length(grep('S', colnames(df)))
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT', 'ECE',
'EE', 'ME'),
Strength = c(80, 75, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# filter rows that contain the string
# 75 in the Strength column
df %>% filter(grepl(75, Strength))
输出:
示例 2:
R
# R program to illustrate
# grep function
# Creating string vector
x <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grep() function
grep("gfg", x, ignore.case = TRUE, value = TRUE)
grep("Bhuwanesh", x, ignore.case = TRUE, value = TRUE)
grep("GeeksforGeeks", x, ignore.case = FALSE, value = FALSE)
grep("Nainwal", x, ignore.case = FALSE, value = FALSE)
输出:
grepl()
如果在向量中找到指定的模式,则 R 语言中的grepl()函数返回值 True,如果未找到,则返回 false。
语法如下,
Syntax: grepl(stringPattern, string, ignore.case=FALSE)
Parameters:
- stringPattern: The string pattern to be searched
- string: character vector on which searching to be performed
- ignore.case: whether to ignore case in the search. Here ignore.case is an optional parameter as is set to FALSE by default.
示例 1:
R
# R program to illustrate
# grepl function
# Initializing a string vector
str <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grepl() function
grepl("GeeksforGeeks", str)
grepl("Bhuwanesh", str)
grepl("gfg", str)
grepl("Nainwal", str)
输出:
示例 2:
R
# R program to illustrate
# grepl function
# Creating string vector
x <- c("GeeksforGeeks", "Bhuwanesh", "Nainwal", "gfg")
# Calling grepl() function
grepl("gfg", x, ignore.case = TRUE)
grepl("Bhuwanesh", x, ignore.case = TRUE)
grepl("GeeksforGeeks", x, ignore.case = TRUE)
grepl("Nainwal", x, ignore.case = TRUE)
输出:
grep() 和 grepl() 的区别
大多数情况下,这两个功能被认为是相同的。尽管这两个函数都用于检查特定模式是否与给定的字符串集合匹配,但它们返回的输出类型不同。
- grep():此函数返回包含模式的字符的索引字符串。
- grepl():如果字符中存在模式,则此函数返回字符串。
例子:
在此示例中,我们使用 grep()函数在数据向量中搜索模式“GeeksforGeeks”,它返回 1,因为该模式位于给定向量中的索引 1 处。此外,我们在同一向量中搜索模式“Bhuwanesh”,但这次使用 grepl()函数,它返回一组布尔值,描述向量的第 i 个元素是否包含此模式。
R
# create a vector of data
data <- c("GeeksforGeeks", "gfg", "Bhuwanesh",
"Nainwal", "Swift")
grep("GeeksforGeeks", data)
grepl("Bhuwanesh", data)
输出:
什么时候应该使用 grep()?
grep 更倾向于根据列名选择选择列。
示例:在此示例中,我们选择了标题名称中包含字符“S”的整个列。
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT',
'ECE', 'EE',
'ME'),
Strength = c(80, 76, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# select columns that contain the string
# 'S' in their name
df %>% select(grep('S', colnames(df)))
输出:
计算包含某个字符串的行数。 grep()函数应该用于计算给定数据框中与某个字符串匹配的行数。
示例:在此示例中,我们计算了标题中包含“S”的行数。
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT', 'ECE',
'EE', 'ME'),
Strength = c(80, 76, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# select and count columns that contain
# the string 'S' in their name
df %>% length(grep('S', colnames(df)))
输出:
什么时候应该使用 grepl()?
grepl() 应该用于过滤包含特定字符串的数据框中的行。
示例:在此示例中,我们根据强度值 75 过滤了行。
R
library(dplyr)
# creating a data frame
df <- data.frame(Department = c('CSE', 'IT', 'ECE',
'EE', 'ME'),
Strength = c(80, 75, 75, 65, 70),
Score = c(75, 70, 65, 60, 60))
# filter rows that contain the string
# 75 in the Strength column
df %>% filter(grepl(75, Strength))
输出: