我有一个包含micro RNA数据的371MB文本文件.基本上,我只想选择那些有人类microRNA信息的行.
我已经使用read.table读取了该文件.通常,我会用sqldf完成我想要的 - 如果它有'like'语法(select*from <>其中miRNA就像'hsa').不幸的是 - sqldf不支持该语法.
我怎么能在R中这样做?我查看了stackoverflow并没有看到如何进行部分字符串匹配的示例.我甚至安装了stringr包 - 但它并不完全符合我的需要.
我想做的是这样的 - 所有选择hsa- *的行.
selectedRows <- conservedData[, conservedData$miRNA %like% "hsa-"]
Run Code Online (Sandbox Code Playgroud)
当然,这是不正确的语法.
有人可以帮我这个吗?非常感谢阅读.
阿斯达
A5C*_*2T1 129
我注意到你%like%在当前的方法中提到了一个函数.我不知道这是否是对%like%"data.table" 的引用,但如果是,你肯定可以按如下方式使用它.
请注意,对象不必是a data.table(但也要记住data.frames和data.tables的子集方法不相同):
library(data.table)
mtcars[rownames(mtcars) %like% "Merc", ]
iris[iris$Species %like% "osa", ]
Run Code Online (Sandbox Code Playgroud)
如果这就是你所拥有的,那么也许你只是混合了行和列位置来分组数据.
如果您不想加载包,可以尝试使用grep()搜索匹配的字符串.以下是mtcars数据集的示例,其中我们匹配行名称包含"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
Run Code Online (Sandbox Code Playgroud)
另一个例子,使用iris搜索字符串的数据集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
Run Code Online (Sandbox Code Playgroud)
对于你的问题尝试:
selectedRows <- conservedData[grep("hsa-", conservedData$miRNA), ]
Run Code Online (Sandbox Code Playgroud)
Sam*_*rke 51
尝试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
...
Run Code Online (Sandbox Code Playgroud)
对过滤变量包含子串"非"的行过滤样本CO2数据集(R附带).您可以调整是否str_detect找到固定匹配或使用正则表达式 - 请参阅stringr包的文档.
use*_*452 20
LIKE 应该在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
Run Code Online (Sandbox Code Playgroud)
另一种选择是简单地使用grepl函数:
df[grepl('er', df$name), ]
CO2[grepl('non', CO2$Treatment), ]
df <- data.frame(name = c('bob','robert','peter'),
id = c(1,2,3)
)
# name id
# 2 robert 2
# 3 peter 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248949 次 |
| 最近记录: |