我有一个名为LeaseDF的数据框。我想要提取所有Team_Code列包含字母"t"的观察值。我使用的简单代码如下所示。不知何故没有返回任何内容。我也尝试了使用grepl函数的for循环和lapply与grepl,但都无济于事。谢谢。
subset <- LeaseDF[grep("^t-", LeaseDF$TEAM_CODE),]
sqldf
包。df <- data.frame(name = c('monday','tuesday','wednesday', 'thursday', 'friday'))
require(sqldf)
# Select specific values from a column i.e., containing letter "t"
sqldf("select * from df where name LIKE '%t%'")
# And output
name
1 tuesday
2 thursday
或者使用grep
df$name[grep("t", df$name) ]
# And output
[1] tuesday thursday
Levels: friday monday thursday tuesday wednesday
# OR use ^t if you want beginning of the string
df[grep("^t", df$name), ]
或者使用grepl
功能,您还可以排除不匹配的观测值。
df[grepl("t", df$name), , drop = FALSE]
# Output
name
2 tuesday
4 thursday