{"id":1015,"date":"2020-06-21T21:41:46","date_gmt":"2020-06-21T13:41:46","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1015"},"modified":"2020-09-20T22:47:49","modified_gmt":"2020-09-20T14:47:49","slug":"r%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=1015","title":{"rendered":"R\u6570\u636e\u5904\u7406-tidyverse"},"content":{"rendered":"\n<p>\u6570\u636e-\u52a0\u8f7d \u9009\u62e9 \u8fc7\u6ee4 \u7f3a\u5931\u503c \u65b0\u53d8\u91cf \u5206\u7ec4 \u8ba1\u7b97<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Install.packages(\u201ctidyverse\u201d)\n> require(tidyverse)\n\u8f7d\u5165\u9700\u8981\u7684\u7a0b\u8f91\u5305\uff1atidyverse\n-- Attaching packages ------------------------------------ tidyverse 1.3.0 --\n\u221a ggplot2 3.3.2     \u221a purrr   0.3.4\n\u221a tibble  3.0.1     \u221a dplyr   1.0.0\n\u221a tidyr   1.1.0     \u221a stringr 1.4.0\n\u221a readr   1.3.1     \u221a forcats 0.5.0\n-- Conflicts --------------------------------------- tidyverse_conflicts() --\nx dplyr::filter() masks stats::filter()\nx dplyr::lag()    masks stats::lag()\n> view(starwars)\n> starwars %>%\n+ select(gender,mass,height,species)\n# A tibble: 87 x 4\n   gender     mass height species\n   &lt;chr>     &lt;dbl>  &lt;int> &lt;chr>  \n 1 masculine    77    172 Human  \n 2 masculine    75    167 Droid  \n 3 masculine    32     96 Droid  \n 4 masculine   136    202 Human  \n 5 feminine     49    150 Human  \n 6 masculine   120    178 Human  \n 7 feminine     75    165 Human  \n 8 masculine    32     97 Droid  \n 9 masculine    84    183 Human  \n10 masculine    77    182 Human  \n# ... with 77 more rows\n> starwars %>%\n+ select(gender,mass,height,species) %>%\n+ filter(species == \"Human\")\n# A tibble: 35 x 4\n   gender     mass height species\n   &lt;chr>     &lt;dbl>  &lt;int> &lt;chr>  \n 1 masculine    77    172 Human  \n 2 masculine   136    202 Human  \n 3 feminine     49    150 Human  \n 4 masculine   120    178 Human  \n 5 feminine     75    165 Human  \n 6 masculine    84    183 Human  \n 7 masculine    77    182 Human  \n 8 masculine    84    188 Human  \n 9 masculine    NA    180 Human  \n10 masculine    80    180 Human  \n# ... with 25 more rows\n\n\n>  starwars %>%\n+ select(gender,mass,height,species) %>%\n+ filter(species == \"Human\") %>%\n+ filter(species == \"Human\") %>%\n+ na.omit()\n# A tibble: 22 x 4\n   gender     mass height species\n   &lt;chr>     &lt;dbl>  &lt;int> &lt;chr>  \n 1 masculine    77    172 Human  \n 2 masculine   136    202 Human  \n 3 feminine     49    150 Human  \n 4 masculine   120    178 Human  \n 5 feminine     75    165 Human  \n 6 masculine    84    183 Human  \n 7 masculine    77    182 Human  \n 8 masculine    84    188 Human  \n 9 masculine    80    180 Human  \n10 masculine    77    170 Human  \n# ... with 12 more rows\n> starwars %>%\n+  select(gender,mass,height,species) %>%\n+  filter(species == \"Human\") %>%\n+  filter(species == \"Human\") %>%\n+  na.omit() %>%\n+ mutate(height=height\/100)\n# A tibble: 22 x 4\n   gender     mass height species\n   &lt;chr>     &lt;dbl>  &lt;dbl> &lt;chr>  \n 1 masculine    77   1.72 Human  \n 2 masculine   136   2.02 Human  \n 3 feminine     49   1.5  Human  \n 4 masculine   120   1.78 Human  \n 5 feminine     75   1.65 Human  \n 6 masculine    84   1.83 Human  \n 7 masculine    77   1.82 Human  \n 8 masculine    84   1.88 Human  \n 9 masculine    80   1.8  Human  \n10 masculine    77   1.7  Human  \n# ... with 12 more rows\n> starwars %>%\n+  select(gender,mass,height,species) %>%\n+  filter(species == \"Human\") %>%\n+  filter(species == \"Human\") %>%\n+  na.omit() %>%\n+ mutate(height=height\/100) %>%\n+ mutate(BMI=mass\/height^2)\n# A tibble: 22 x 5\n   gender     mass height species   BMI\n   &lt;chr>     &lt;dbl>  &lt;dbl> &lt;chr>   &lt;dbl>\n 1 masculine    77   1.72 Human    26.0\n 2 masculine   136   2.02 Human    33.3\n 3 feminine     49   1.5  Human    21.8\n 4 masculine   120   1.78 Human    37.9\n 5 feminine     75   1.65 Human    27.5\n 6 masculine    84   1.83 Human    25.1\n 7 masculine    77   1.82 Human    23.2\n 8 masculine    84   1.88 Human    23.8\n 9 masculine    80   1.8  Human    24.7\n10 masculine    77   1.7  Human    26.6\n# ... with 12 more rows\n> starwars %>%\n+  select(gender,mass,height,species) %>%\n+  filter(species == \"Human\") %>%\n+  filter(species == \"Human\") %>%\n+  na.omit() %>%\n+ mutate(height=height\/100) %>%\n+ mutate(BMI=mass\/height^2) %>%\n+ group_by(gender) %>%\n+ summarise(Average_BMI=mean(BMI))\n`summarise()` ungrouping output (override with `.groups` argument)\n# A tibble: 2 x 2\n  gender    Average_BMI\n  &lt;chr>           &lt;dbl>\n1 feminine         22.0\n2 masculine        26.0\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e-\u52a0\u8f7d \u9009\u62e9 \u8fc7\u6ee4 \u7f3a\u5931\u503c \u65b0\u53d8\u91cf \u5206\u7ec4 \u8ba1\u7b97<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1015","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1015"}],"version-history":[{"count":2,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1015\/revisions"}],"predecessor-version":[{"id":1282,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1015\/revisions\/1282"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1015"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}