{"id":956,"date":"2023-07-28T04:57:15","date_gmt":"2023-07-28T04:57:15","guid":{"rendered":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/"},"modified":"2023-07-28T04:57:15","modified_gmt":"2023-07-28T04:57:15","slug":"grep-%e4%b8%8e-grepl-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/","title":{"rendered":"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u4eba\u4eec\u5728 R \u4e2d\u7ecf\u5e38\u6df7\u6dc6\u7684\u4e24\u4e2a\u51fd\u6570\u662f<strong>grep()<\/strong>\u548c<strong>grepl()<\/strong> \u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u5141\u8bb8\u60a8\u67e5\u770b\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u79cd\u6a21\u5f0f\uff0c\u4f46\u5b83\u4eec\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u5f53\u5b57\u7b26\u4e32\u4e2d\u5b58\u5728\u6a21\u5f0f\u65f6\uff0c <strong>grepl()<\/strong>\u8fd4\u56de TRUE\u3002<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>grep()<\/strong>\u8fd4\u56de\u5305\u542b\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u7d22\u5f15\u5411\u91cf\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">\u4ee5\u4e0b\u793a\u4f8b\u8bf4\u660e\u4e86\u8fd9\u79cd\u5dee\u5f02\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create a vector of data<\/span>\ndata &lt;- c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center')\n\n<span style=\"color: #3366ff;\">grep<\/span> ('Guard', data)\n[1] 1 2\n\n<span style=\"color: #3366ff;\">grepl<\/span> ('Guard', data) \n[1] TRUE TRUE FALSE FALSE FALSE<\/strong><\/pre>\n<p><span style=\"color: #000000;\">\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u4e86\u60a8\u4f55\u65f6\u53ef\u80fd\u60f3\u8981\u4f7f\u7528\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u800c\u4e0d\u662f\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/span><\/p>\n<h3><span style=\"color: #000000;\"><strong>\u4f55\u65f6\u4f7f\u7528 grep()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><strong>1. \u8fc7\u6ee4\u5305\u542b\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u884c<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>grepl()<\/strong>\u6700\u5e38\u89c1\u7684\u7528\u9014\u4e4b\u4e00\u662f\u8fc7\u6ee4\u6570\u636e\u6846\u4e2d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u884c\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\">library(dplyr)<\/span>\n\n#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#filter rows that contain the string 'Guard' in the player column\n<\/span><span style=\"color: #000000;\">df %&gt;% filter( <span style=\"color: #3366ff;\">grepl<\/span> ('Guard', player))\n\n   player points rebounds\n1 P Guard 12 5\n2 S Guard 15 7<\/span><\/strong><\/pre>\n<p><span style=\"color: #000000;\"><strong>\u76f8\u5173\uff1a<\/strong><a href=\"https:\/\/statorials.org\/cn\/\u8fc7\u6ee4\u5305\u542b-dplyr-\u5b57\u7b26\u4e32\u7684\u884c\/\">\u5982\u4f55\u4f7f\u7528 dplyr \u8fc7\u6ee4\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u884c<\/a><\/span><\/p>\n<h3><span style=\"color: #000000;\"><strong>\u4f55\u65f6\u4f7f\u7528 grep()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><strong>1. \u9009\u62e9\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u5217<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u60a8\u53ef\u4ee5\u4f7f\u7528<strong>grep()<\/strong>\u9009\u62e9\u6570\u636e\u6846\u4e2d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u5217\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\">library(dplyr)<\/span>\n\n#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#select columns that contain the string 'p' in their name\n<\/span><span style=\"color: #000000;\">df %&gt;% select( <span style=\"color: #3366ff;\">grep<\/span> ('p', colnames(df)))\n\n     player points\n1 P Guard 12\n2 S Guard 15\n3S Forward 19\n4P Forward 22\n5 Center 32<\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>2. \u7edf\u8ba1\u5305\u542b\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u884c\u6570<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u60a8\u53ef\u4ee5\u4f7f\u7528<strong>grep()<\/strong>\u6765\u8ba1\u7b97\u6570\u636e\u6846\u4e2d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u884c\u6570\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create data frame<\/span>\ndf &lt;- data.frame(player = c('P Guard', 'S Guard', 'S Forward', 'P Forward', 'Center'),\n                 points = c(12, 15, 19, 22, 32),\n                 rebounds = c(5, 7, 7, 12, 11))\n\n<span style=\"color: #008080;\">#count how many rows contain the string 'Guard' in the player column\n<\/span><span style=\"color: #000000;\">length( <span style=\"color: #3366ff;\">grep<\/span> ('Guard', df$player))\n\n[1] 2<\/span><\/strong><\/pre>\n<p><span style=\"color: #000000;\"><em>\u60a8\u53ef\u4ee5<a href=\"https:\/\/statorials.org\/cn\/\u7edf\u8ba1\u5b66\u4ee5\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u89e3\u91ca\u6982\u5ff5\uff0c\u6211\u4eec\u4f7f\u5b66\u4e60\u7edf\u8ba1\u53d8\u5f97\u66f4\u5bb9\u6613\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728\u6b64\u5904<\/a>\u627e\u5230\u66f4\u591a R \u6559\u7a0b\u3002<\/em><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eba\u4eec\u5728 R \u4e2d\u7ecf\u5e38\u6df7\u6dc6\u7684\u4e24\u4e2a\u51fd\u6570\u662fgrep()\u548cgrepl() \u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u5141\u8bb8\u60a8\u67e5\u770b\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u79cd\u6a21 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-956","post","type-post","status-publish","format-standard","hentry","category-11"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/title>\n<meta name=\"description\" content=\"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/cn\/grep-\u4e0e-grepl-r\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f\" \/>\n<meta property=\"og:description\" content=\"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/cn\/grep-\u4e0e-grepl-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T04:57:15+00:00\" \/>\n<meta name=\"author\" content=\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/\",\"url\":\"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/\",\"name\":\"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/cn\/#website\"},\"datePublished\":\"2023-07-28T04:57:15+00:00\",\"dateModified\":\"2023-07-28T04:57:15+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261\"},\"description\":\"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u5bb6\",\"item\":\"https:\/\/statorials.org\/cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/cn\/#website\",\"url\":\"https:\/\/statorials.org\/cn\/\",\"name\":\"Statorials\",\"description\":\"\u60a8\u7684\u7edf\u8ba1\u7d20\u517b\u6307\u5357\uff01\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261\",\"name\":\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\"},\"description\":\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u672c\u6770\u660e\uff0c\u4e00\u4f4d\u9000\u4f11\u7684\u7edf\u8ba1\u5b66\u6559\u6388\uff0c\u540e\u6765\u6210\u4e3a Statorials \u7684\u70ed\u5fc3\u6559\u5e08\u3002 \u51ed\u501f\u5728\u7edf\u8ba1\u9886\u57df\u7684\u4e30\u5bcc\u7ecf\u9a8c\u548c\u4e13\u4e1a\u77e5\u8bc6\uff0c\u6211\u6e34\u671b\u5206\u4eab\u6211\u7684\u77e5\u8bc6\uff0c\u901a\u8fc7 Statorials \u589e\u5f3a\u5b66\u751f\u7684\u80fd\u529b\u3002\u4e86\u89e3\u66f4\u591a\",\"sameAs\":[\"https:\/\/statorials.org\/cn\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f","description":"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/cn\/grep-\u4e0e-grepl-r\/","og_locale":"zh_CN","og_type":"article","og_title":"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f","og_description":"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002","og_url":"https:\/\/statorials.org\/cn\/grep-\u4e0e-grepl-r\/","og_site_name":"Statorials","article_published_time":"2023-07-28T04:57:15+00:00","author":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/","url":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/","name":"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f","isPartOf":{"@id":"https:\/\/statorials.org\/cn\/#website"},"datePublished":"2023-07-28T04:57:15+00:00","dateModified":"2023-07-28T04:57:15+00:00","author":{"@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261"},"description":"R \u4e2d grep() \u548c grepl() \u51fd\u6570\u4e4b\u95f4\u5dee\u5f02\u7684\u7b80\u5355\u89e3\u91ca\u3002","breadcrumb":{"@id":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/cn\/grep-%e4%b8%8e-grepl-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u5bb6","item":"https:\/\/statorials.org\/cn\/"},{"@type":"ListItem","position":2,"name":"R \u4e2d grep() \u548c grepl() \u7684\u6bd4\u8f83\uff1a\u6709\u4ec0\u4e48\u533a\u522b\uff1f"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/cn\/#website","url":"https:\/\/statorials.org\/cn\/","name":"Statorials","description":"\u60a8\u7684\u7edf\u8ba1\u7d20\u517b\u6307\u5357\uff01","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261","name":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a"},"description":"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u672c\u6770\u660e\uff0c\u4e00\u4f4d\u9000\u4f11\u7684\u7edf\u8ba1\u5b66\u6559\u6388\uff0c\u540e\u6765\u6210\u4e3a Statorials \u7684\u70ed\u5fc3\u6559\u5e08\u3002 \u51ed\u501f\u5728\u7edf\u8ba1\u9886\u57df\u7684\u4e30\u5bcc\u7ecf\u9a8c\u548c\u4e13\u4e1a\u77e5\u8bc6\uff0c\u6211\u6e34\u671b\u5206\u4eab\u6211\u7684\u77e5\u8bc6\uff0c\u901a\u8fc7 Statorials \u589e\u5f3a\u5b66\u751f\u7684\u80fd\u529b\u3002\u4e86\u89e3\u66f4\u591a","sameAs":["https:\/\/statorials.org\/cn"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts\/956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/comments?post=956"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts\/956\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/media?parent=956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/categories?post=956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/tags?post=956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}