简体   繁体   English

R子集一个数据帧,不包括列中的某些值

[英]R subsetting a Data Frame excluding certain values from Column

i have a data frame that has certain values ending in (SP1) or (SP2) along with other values that do not end with (SP1) or (SP2) . 我有一个数据帧,该数据帧具有以(SP1)(SP2)结尾的某些值以及不以(SP1)(SP2)结尾的其他值。 what i am trying to accomplish is to create a new data frame that contains only the values that do not end on (SP1) or (SP2) 我要完成的工作是创建一个仅包含不以(SP1)(SP2)结尾的值的新数据框

i can subset the values containing (SP1) or (SP2) as below: 我可以如下子集包含(SP1)(SP2)的值:

Loc.Z.End = subset(location.simple, grepl("(SP1)|(SP2)", location.simple$Location_Name))

i thought that adding the ! 我以为加了! operator would do the trick as below but it does not! 操作员将按照以下方法完成操作,但不会!

Loc.Z.End = subset(location.simple, [!grepl("(SP1)|(SP2)", location.simple$Location_Name)])

but this does not do the trick! 但这并不能解决问题! what am i doing wrong? 我究竟做错了什么?

here is an exaple of the data 这是数据的一个例子

Location_Name City AMEM01 London BANY01 Leeds HSBC Dubai (SP1) Dubai Leeds(SP2) Leeds

Thanks for your help! 谢谢你的帮助!

The argument that takes the grepl logical expression is subset and according to ?subset 采用grepl逻辑表达式的参数是subset并根据?subset

subset - logical expression indicating elements or rows to keep: missing values are taken as false. 子集-表示要保留的元素或行的逻辑表达式:缺失值被视为false。

We don't need any [] 我们不需要任何[]

subset(location.simple, !grepl("(SP1)|(SP2)", location.simple$Location_Name))
#  Location_Name   City
#1        AMEM01 London
#2        BANY01  Leeds

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

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