简体   繁体   English

根据条件删除选定的案例

[英]Remove selected cases based on a criteria

I would like to remove cases from a data frame based on whether they contain a particular pattern. 我想根据案例是否包含特定模式从数据框中删除案例。 For example in the data frame below I would like to remove all the rows that contain (Intercept), iyeareducc, ibphtdep and gender_R22 (or alternatively selecting the rows that contain _carrier1 or adri). 例如,在下面的数据框中,我想删除所有包含(Intercept),iyeareducc,ibphtdep和gender_R22的行(或者选择包含_carrier1或adri的行)。

                               OR        CI    P
apoee4_carrier.(Intercept)     1.96 0.97-3.94 0.06
apoee4_carrier.apoee4_carrier1 1.03 0.77-1.37 0.84
apoee4_carrier.iyeareducc      0.86  0.82-0.9 0.00
apoee4_carrier.ibphdtdep       1.01 0.96-1.05 0.81
apoee4_carrier.gender_R22      0.87 0.67-1.12 0.28
BDNF_carrier.(Intercept)       2.05 1.01-4.14 0.04
BDNF_carrier.BDNF_carrier1     0.87 0.66-1.14 0.33
BDNF_carrier.iyeareducc        0.86  0.82-0.9 0.00
BDNF_carrier.ibphdtdep         1.00 0.96-1.05 0.82
BDNF_carrier.gender_R22        0.87 0.67-1.12 0.28
adri.(Intercept)               1.60 0.78-3.31 0.20
adri.adri                      1.03    1-1.06 0.04
adri.iyeareducc                0.89 0.84-0.94 0.00
adri.ibphdtdep                 1.00 0.95-1.04 0.87
adri.gender_R22                0.87 0.67-1.12 0.27

While I could use a sequence to subset out the rows I require, like so 虽然我可以使用序列来子集出我需要的行,像这样

dat[(seq(2,nrow(dat),5)),]
                                 OR        CI    P
apoee4_carrier.apoee4_carrier1 1.03 0.77-1.37 0.84
BDNF_carrier.BDNF_carrier1     0.87 0.66-1.14 0.33
adri.adri                      1.03    1-1.06 0.04

this will only work if the sequence is the same throughout the entire dataframe, which may not be necessarily the case as this data frame is created from a list of data frames that have been rbind together. 仅当整个数据帧中的序列相同时,这才起作用。由于此数据帧是根据已被绑定在一起的数据帧列表创建的,因此不一定是这种情况。

Thanks. 谢谢。

You can use grep to select the rows you want/don't want: 您可以使用grep选择想要/不需要的行:

dat[-grep("Intercept|iyeareducc|ibphdtdep|gender", rownames(dat)),]

grep returns the row numbers of the rows for which the row names contain at least one of your search strings (the | between each string means "OR"). grep返回其行名包含至少一个搜索字符串的行的行号(每个字符串之间的|表示“ OR”)。 Putting a minus sign in front of grep tells R to return only the rows of dat that are not returned by grep . grep前面加上减号会告诉R仅返回grep未返回的dat行。

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

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