过滤/子集/删除包含R中字符串中间字符的行(filter/subset/delete rows that contain character in middle of string in R)
我有一个包含肽序列的数据框,我只想保留其字符串中没有内部“R”或“K”的行。
df1 <- data.frame( Peptide = c("ABCOIIJUHFSAUJHR", "AOFIAUKOAISDFUK", 'ASOIRDFHAOHFKK')) df1 #check output
作为输出,我只想保留第一行(即“ABCOIIJUHFSAUJHR”)。
我曾尝试使用过滤器(dplyr)和str_locate_all从字符串包和长度,但无法弄清楚。
任何帮助将非常感激。
谢谢Moe
I've got a dataframe with a column containing peptide sequences and I want to keep only rows that have no internal "R" or "K" in their string.
df1 <- data.frame( Peptide = c("ABCOIIJUHFSAUJHR", "AOFIAUKOAISDFUK", 'ASOIRDFHAOHFKK')) df1 #check output
As output I would like to keep only the first row (i.e. "ABCOIIJUHFSAUJHR").
I have tried using filter (dplyr) and str_locate_all from the stringr package and length but couldn't figure it out.
Any help would be much appreciated.
Thanks Moe
原文:https://stackoverflow.com/questions/49933741
最满意答案
NSDate
本身不能用JSON表示。 正如NSJSONSerialization
文档所说:可以转换为JSON的对象必须具有以下属性:
顶级对象是
NSArray
或NSDictionary
。所有对象都是
NSString
,NSNumber
,NSArray
,NSDictionary
或NSNull
。所有字典键都是
NSString
实例。数字不是NaN或无穷大。
从您的问题中不清楚日期应该以什么格式表示在您的JSON中。 通常,您以某种标准日期字符串格式格式化日期(例如ISO 8601 / RFC 3339格式,如
2016-01-25T06:54:00Z
)。NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
或者,对于macOS 10.12和iOS 10,您可以:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
然后:
NSString *birthDateString = [formatter stringFromDate:birthDate];
有关详细信息,请参阅Apple技术问答1480 。
或许,因为这是生日,你只需要
yyyy-MM-dd
。NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *birthDateString = [formatter stringFromDate:birthDate];
最重要的是,您必须告诉我们您的Web服务期望格式化日期的格式,然后我们可以帮助您相应地格式化它。
NSDate
cannot be represented in JSON natively. AsNSJSONSerialization
documentation says:An object that may be converted to JSON must have the following properties:
The top level object is an
NSArray
orNSDictionary
.All objects are instances of
NSString
,NSNumber
,NSArray
,NSDictionary
, orNSNull
.All dictionary keys are instances of
NSString
.Numbers are not NaN or infinity.
It's unclear from your question in what format the date should be represented in your JSON. Often, you format dates in some standard date string format (e.g. ISO 8601/RFC 3339 format like
2016-01-25T06:54:00Z
).NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
Or, for macOS 10.12 and iOS 10, you can do:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
Then:
NSString *birthDateString = [formatter stringFromDate:birthDate];
See Apple Technical Q&A 1480 for more information.
Or perhaps, because it's a birthday, you only need
yyyy-MM-dd
.NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *birthDateString = [formatter stringFromDate:birthDate];
Bottom line, you have to tell us what format your web service is expecting the dates to be formatted, and then we can help you format it accordingly.
相关问答
更多-
你实现了NSURLConnectionDelagate的所有委托方法吗?看起来你正在从“ - (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)数据”delagate方法转换数据。 如果是这样,您可能会收到不完整的数据,并且无法转换。 尝试这个: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // ...
-
你的json对象是一个使用NSArray而不是NSDictionary的数组。 Your json object is an array try with NSArray instead of NSDictionary.
-
NSDate本身不能用JSON表示。 正如NSJSONSerialization文档所说: 可以转换为JSON的对象必须具有以下属性: 顶级对象是NSArray或NSDictionary 。 所有对象都是NSString , NSNumber , NSArray , NSDictionary或NSNull 。 所有字典键都是NSString实例。 数字不是NaN或无穷大。 从您的问题中不清楚日期应该以什么格式表示在您的JSON中。 通常,您以某种标准日期字符串格式格式化日期(例如ISO 8601 / RFC ...
-
如何解析jsonData?(How to parse jsonData?)[2024-04-17]
你的json不正确 { "type": "1", "item": "Order created successfully.", "order_id": "7" } { "multicast_id": 9215180185089775977, "success": 1, "failure": 0, "canonical_ids": 0, "results": [{ "message_id": "0:1465191236656122%86acb02ff9fd7ecd" }] } 例如,您忘记了逗 ... -
如何将NSDictionary转换为NSData,反之亦然?(How can I convert NSDictionary to NSData and vice versa?)[2019-11-22]
NSDictionary - > NSData: NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; NSData - > NSDictionary: NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData]; NSDictionary -> NSData: NSData ... -
是的, NSDate对象可以用作NSDictionary的键 - 只要它支持NSCopying协议,任何对象类型都可以用作键。 使用isEqual:而不是指针值来比较密钥。 有关更多详细信息,请参阅NSDictionary文档的“概述”部分。 Yes, NSDate objects can be used as keys for NSDictionary - any object type can be used as a key provided it supports the NSCopying pro ...
-
错误消息说: 最后的垃圾。 因此,比较您的数据和服务器上的数据,我们看到: Test string data: <7b226572 726f7222 3a7b2270 61737377 6f726422 3a5b22d0 9dd0b5d0 bfd180d0 b0d0b2d0 b8d0bbd1 8cd0bdd1 8bd0b920 656d6169 6c20d0b8 d0bbd0b8 20d0bfd0 b0d180d0 bed0bbd1 8c225d7d 7d> Server data: <7b226 ...
-
在swift4中将JSONresponse转换为NSDATA或NSDICTIONARY(convert JSONresponse to NSDATA or NSDICTIONARY in swift4)[2022-02-21]
您已经在使用Alamofire作为网络请求者,因此没有必要将响应对象转换为JSON对象或Data对象。 库本身将提供解析的JSON对象。 因此,请检查以下示例代码以检查JSON对象。 let urlString = "https://......" let securityToken: String = "Basic YWFnZTQxNDAxMjgwODYyNDk3NWFiYWNhZjlhNjZjMDRlMWY6ODYyYTk0NTFhYjliNGY1M2EwZWJiOWI2ZWQ1ZjYw ... -
在使用NSJSONSerialization解析NSData之前,必须将NSData转换为UTF8。 NSError* error = nil; NSString *strISOLatin = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding]; NSData *dataUTF8 = [strISOLatin dataUsingEncoding:NSUTF8StringEncoding]; id dict = [ ...
-
在第一种情况下,您将打印出JSON编码操作的结果。 在第二种情况下,您将通过NSLog而不是其JSON表示直接打印出字典。 更新: 要查看此内容,请使用此日志记录功能而不是NSLog : void logThisObject(id obj) { if(obj == nil) { NSLog(@"logObject: nil"); } else { NSString *className = NSStringFromClass([obj class]); ...