use*_*990 62 python string r startswith ends-with
正在寻找名称以某些子字符串开头的预测变量,找不到任何类似的函数.
ijo*_*eph 85
正如在3.3.0中添加的base
那样,startsWith
(和endsWith
)就是这样.
> startsWith("what", "wha")
[1] TRUE
> startsWith("what", "ha")
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
https://stat.ethz.ch/R-manual/R-devel/library/base/html/startsWith.html
mat*_*fee 24
不像那样内置.
选项包括grepl
和substr
.
x <- 'ABCDE'
grepl('^AB', x) # starts with AB?
grepl('DE$', x) # ends with DE?
substr(x, 1, 2) == 'AB'
substr('ABCDE', nchar(x)-1, nchar(x)) == 'DE'
Run Code Online (Sandbox Code Playgroud)
G. *_*eck 11
dplyr包的select
声明支持starts_with
和ends_with
.例如,这将选择以#开头的虹膜数据帧的列Petal
library(dplyr)
select(iris, starts_with("Petal"))
Run Code Online (Sandbox Code Playgroud)
select
也支持其他子命令.试试?select
.
小智 6
我能想到的最简单的方法是使用%like%
运算符:
library(data.table)
"foo" %like% "^f"
Run Code Online (Sandbox Code Playgroud)
评估为TRUE
- 从f开始
"foo" %like% "o$"
Run Code Online (Sandbox Code Playgroud)
评估为TRUE
- 结束o
"bar" %like% "a"
Run Code Online (Sandbox Code Playgroud)
评估为TRUE
- 包含a