简体   繁体   English

R从data.frame中选择特定的行间隔

[英]R Select certain interval of rows from data.frame

I tried to find a solution over google but after 5 hrs I hope I can find an answer from the community, so thank you already. 我试图在Google上找到解决方案,但是5小时后,我希望可以从社区中找到答案,所以已经谢谢您了。

Im having a data.frame: 我有一个data.frame:

    > out.allpheno["MAP2K2"]
            MAP2K2
c1m2      5.55e-02
c1.loc42  4.32e-02
c1.loc52  6.04e-02
c1m10     3.19e-01
c1.loc103 6.12e-01
c1.loc107 8.65e-01
c1m12     1.03e+00
c1.loc113 1.04e+00
c1.loc117 1.07e+00
c1.loc128 7.91e-01
c1m14     4.92e-01
c1.loc134 5.65e-01
c1.loc135 6.10e-01
c1.loc157 3.63e-01
c1m16     1.81e-01
c1.loc177 5.61e-02
c1m18     9.03e-02
c1.loc182 1.08e-01
c1m19     2.38e-01
c1m22     9.20e-02
c1.loc219 1.03e-01
c1m24     1.62e-01
c1.loc236 3.83e-01
c1.loc240 7.45e-01
c1.loc262 2.00e+00
c1.loc267 2.41e+00
c1.loc268 2.48e+00
c1.loc269 2.53e+00
c1.loc270 2.56e+00
c1m28     2.57e+00
c1.loc272 2.52e+00
c1.loc273 2.46e+00
c1.loc274 2.37e+00
c1.loc276 2.12e+00
c1m30     9.63e-01
c1m31     1.89e+00
c2m1      5.85e-01
c2.loc11  5.27e-01
c2m5      5.51e-01
c2m6      5.15e-01
c2.loc52  6.56e-01
c2m7      1.05e+00
c2.loc59  1.07e+00
c2.loc62  1.05e+00
c2.loc72  4.90e-01
c2.loc91  3.63e-01
c2m11     3.63e-01
c2.loc103 3.08e-01
c2.loc114 8.62e-02
c2m14     2.99e-02
c2.loc129 3.15e-02
c2.loc151 2.95e-02
c2.loc167 2.21e-01
c2.loc199 1.49e+00
c2.loc200 1.53e+00
c2.loc201 1.56e+00
c2.loc203 1.61e+00
c2.loc204 1.63e+00
c2m21     1.63e+00
c2.loc214 1.32e+00
c2.loc218 1.16e+00
c2.loc229 3.23e-01
c2.loc237 7.70e-03
c2.loc241 2.57e-02
c2m25     3.81e-02
c3.loc1   8.17e-01
c3.loc4   6.51e-01
c3.loc32  2.95e-01
c3.loc35  3.14e-01
c3m6      8.02e-01
c3.loc54  9.58e-01
c3.loc71  1.48e+00
c3.loc73  1.47e+00
c3m9      1.00e+00
c3.loc89  2.55e-01
c3m10     1.47e-01
c3.loc94  2.84e-01
c3m14     2.37e-01
c3m15     9.24e-04
c3.loc152 2.47e-02
c3m16     4.88e-02
c3.loc175 3.69e-05
c3.loc180 6.16e-02
c3m20     4.87e-01
c3.loc199 4.08e-01
c3m25     8.00e-02
c3.loc248 1.60e-01
c3m26     2.66e-01
c3.loc268 1.53e+00
c3.loc271 1.85e+00
c3.loc273 1.95e+00
c3m28     1.97e+00
c3m29     1.01e+00
c4m4      8.34e-03
c4.loc39  3.94e-03
c4m5      2.81e-03
c4.loc48  1.92e-02
c4.loc57  2.08e-02
c4m7      8.11e-03
c4.loc67  5.92e-03
c4m9      6.74e-02
c4m10     5.28e-01
c4m12     8.52e-01
c4.loc121 2.97e-01
c4m17     5.38e-02
c4m20     1.88e-02
c4m22     3.58e-01
c4m24     1.66e-02
c4.loc236 1.68e-02
c4.loc288 1.62e-01
c4.loc295 1.72e-01
c4m31     1.72e-01
c4.loc313 4.18e-03

I want to retrieve the rows lets say from c1m2 to c2m25. 我想检索从c1m2到c2m25的行。 Is there any build-in-function to achieve this or do I need to create a loop and check in every row for the elements I want to include? 是否有任何内置函数可以实现此目的,或者是否需要创建一个循环并在每一行中检查要包含的元素? Thank you 谢谢

An ordered factor lets you use >= and <= to compare to character values, defining a custom order (ie, other than alphabetic order). 有序因子使您可以使用>=<=与字符值进行比较,从而定义自定义顺序(即,字母顺序除外)。

This is a shorter subset than you requested, but you can use any values for bounds: 这是一个比您请求的子集短的子集,但是您可以使用任何值作为界限:

f <- factor(rownames(out.allpheno), levels=rownames(out.allpheno), ordered=TRUE)
out.allpheno[f >= 'c1m14' & f <= 'c1m16', ,drop=FALSE]

##           MAP2K2
## c1m14      0.492
## c1.loc134  0.565
## c1.loc135  0.610
## c1.loc157  0.363
## c1m16      0.181

If you already have them all in order, then, assuming your data.frame is df 如果已经将它们全部整理好,那么假设您的data.framedf

idx1 <- which(row.names(df) == "c1m2")
idx2 <- which(row.names(df) == "c1m25")
df[idx1:idx2, ]

Another potential solution, using regular expressions: 另一个可能的解决方案,使用正则表达式:

my_regex <- "^c[12]"
matched_rownames <- grep( my_regex, rownames(df), value=TRUE )
df[ rownames(df) %in% matched_rownames, , drop=FALSE ]

This will be handy in the case that you're sure the row names do follow a regular structure but they may be out of order, and you know you just want to grab any rows that start with c1 or c2 . 如果您确定行名确实遵循常规结构,但是它们可能不正确,这很方便,并且您知道只想获取以c1c2开头的任何行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM