3 回答

TA貢獻1801條經(jīng)驗 獲得超8個贊
我注意到你%like%在當前的方法中提到了一個函數(shù)。我不知道這是否是對%like%“data.table” 的引用,但如果是,你肯定可以按如下方式使用它。
請注意,對象不必是a data.table(但也要記住data.frames和data.tables的子集方法不相同):
library(data.table)
mtcars[rownames(mtcars) %like% "Merc", ]
iris[iris$Species %like% "osa", ]
如果這就是你所擁有的,那么也許你只是混合了行和列位置來分組數(shù)據(jù)。
如果您不想加載包,可以嘗試使用grep()搜索匹配的字符串。以下是mtcars數(shù)據(jù)集的示例,其中我們匹配行名稱包含“Merc”的所有行:
mtcars[grep("Merc", rownames(mtcars)), ]
mpg cyl disp hp drat wt qsec vs am gear carb
# Merc 240D 24.4 4 146.7 62 3.69 3.19 20.0 1 0 4 2
# Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
# Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
# Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
# Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
# Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
# Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.0 0 0 3 3
另一個例子,使用iris搜索字符串的數(shù)據(jù)集osa:
irisSubset <- iris[grep("osa", iris$Species), ]
head(irisSubset)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 5.1 3.5 1.4 0.2 setosa
# 2 4.9 3.0 1.4 0.2 setosa
# 3 4.7 3.2 1.3 0.2 setosa
# 4 4.6 3.1 1.5 0.2 setosa
# 5 5.0 3.6 1.4 0.2 setosa
# 6 5.4 3.9 1.7 0.4 setosa
對于你的問題嘗試:
selectedRows <- conservedData[grep("hsa-", conservedData$miRNA), ]

TA貢獻2003條經(jīng)驗 獲得超2個贊
嘗試str_detect()使用stringr包,它檢測字符串中是否存在模式。
下面是還采用了一種方法%>%管和filter()從dplyr包:
library(stringr)
library(dplyr)
CO2 %>%
filter(str_detect(Treatment, "non"))
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
...
對過濾變量包含子串“非”的行過濾樣本CO2數(shù)據(jù)集(R附帶)。您可以調(diào)整是否str_detect找到固定匹配或使用正則表達式 - 請參閱stringr包的文檔。

TA貢獻1780條經(jīng)驗 獲得超5個贊
LIKE 應(yīng)該在sqlite中工作:
require(sqldf)
df <- data.frame(name = c('bob','robert','peter'),id=c(1,2,3))
sqldf("select * from df where name LIKE '%er%'")
name id
1 robert 2
2 peter 3
- 3 回答
- 0 關(guān)注
- 562 瀏覽
添加回答
舉報