Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to merge dplyr in R language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to merge dplyr in R language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Conversion and merger

Left_join (a, b, by = "x1"): adds matching dataset b records (rows) to dataset a.

Right_join (a, b, by = "x1"): adds a matching dataset a record to dataset b.

Inner_join (a, b, by = "x1"): merge data. Only matching records are retained.

Full_join (a, b, by = "x1"): merge data. Keep all records, all lines. Those that are not available are filled with NA

Library (dplyr) a=data.frame (x1roomc ("A", "B", "C"), x2folc (1:3)) b=data.frame (x1folc ("A", "B", "D"), x3folc ("T", "F", "T")) a Matching dataset b records are added to dataset a by adding matching dataset b records to dataset a # # x1 x2 x3 records # 1 A 1 records # 2 B 2 fills # 3 C 3 right_join (a, b, by = "x1") # add a matching dataset a record to dataset b. # # x1 x2 x3 NA Tinner_join # 1 A 1 threading # 2 B 2 fusing # 3 D NA Tinner_join (a, b, by = "x1") # merge data. Only matching records are retained. # # x1 x2 x3 Ffull_join # 1 A 1 tweak # 2 B 2 Ffull_join (a, b, by = "x1") # merge data. Keep all records, all rows, none filled with NA # # x1 x2 x3 lines # 1 A 1 thread # 2 B 2 records # 3 C 3 lines # 4 D NA T2, filter and merge

Semi_join (a, b, by = "x1"): the record in dataset a that matches dataset b.

Anti_join (a, b, by = "x1"): records in dataset a that do not match dataset b

3. Set operation

Intersect (x, y): records that both appear in datasets x and y. Xy intersection

Union (x, y): a record that appears in the dataset x or y. Xy union

Setdiff (x, y): records that appear only in dataset x but not in y. The difference between x and y

Bind_rows (x, y): add dataset y to x as a new deleted row.

Bind_cols (x, y): add dataset y to x as a new column.

Note: the data matches according to the location.

Unlike rbind () and cbind (), rbind () / cbind () has strict requirements for data merging.

X=data.frame ("a", "b", "c"), Bracc ("t", "u", "v"), Centrc (1:3)) y=data.frame ("a", "b", "d"), Benec ("t", "u", "v"), Drunc (2:4) x The columns are all displayed, and the missing ones are filled with NA. Select the parameter .id = "name", and add one more column. Column name name## A B C data # 1 a t 1 NA## 2 b u 2 NA## 3 c v 3 NA## 4 a t NA 2 colors # 5 b u NA 3 steps # 6 d v NA 4bind_rows (x, yma.id = "name") # in order to distinguish that the data is from two other databases You can use id to distinguish # # name A B C dashes # 1 a t 1 NA## 2 1 b u 2 NA## 3 c v 3 NA## 4 2 a t NA 2 columns # 52 b u NA 3 columns # 6 d v NA 4bind_cols (x, y) # # A.. .1 C A.. 4 B.. 5 Duan # 1a t 2a t 2b u 2b u 3cm # 3c v 3d v 44, data sort

Arrange (.data, … , .by _ group = FALSE): sort by variable + default ascending order, add desc () before variable becomes descending order + .by _ group = FALSE ignore ascending sort of group by default, set to TRUE to sort the group in ascending order and then sort by comparison variable

Arrange (mtcars, cyl Desc (disp)) # sort by variable # # mpg cyl disp hp drat wt qsec vs am gear carb## Merc 240D 24.4 146.7 62 3.69 3.190 20.00 1 0 42 # # Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 42 # # Volvo 142E 21.4 4 121 .0109 4.11 2.780 18.60 11 4 2 # Porsche 914-226.0 4 120.3 91 4.43 2.140 16.70 01 5 2 Toyota Corona 21.54 120.1 97 3.70 2.465 20.01 10 3 1 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 11 4 1 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 11 52 # # Fiat X1-9 27.3 27.3 79.0 66 4.08 1.935 18.90 11 4 1 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 11 4 1 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 11 4 1## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 10 3 1## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 10 3 1## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 10 44 # # Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 10 44 # # Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 44 # # Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 44 # # Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 15 6## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 00 34 # # Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 00 34 # # Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 00 34 # # Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 15 4## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 01 5 8## Merc 450SE 16 .4 8 275.8 275.8 3.07 4.070 17.40 00 3 3 Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 00 3 3 days # Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 3 3by_cyl% group_by (cyl) by_cyl% >% arrange (desc (wt) .by _ group = TRUE) # ignores the ascending sort of groups by default. If set to TRUE, the groups will be sorted in ascending order and then sorted by comparison variables # A tibble: 32 x 11 ordered # # Groups: cyl [3] # # mpg cyl disp hp drat wt qsec vs am gear carb## 1 24.4 4147. 62 3.69 3.19 20 1 0 4 2## 2 22.8 4 141. 95 3.92 3.15 22.9 10 4 2## 3 21.4 4 121 109 4.11 2.78 18.6 11 4 2## 4 21.5 4 120. 97 3.7 2.46 20.0 10 3 1## 5 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1## 6 32.4 4 78.7 66 4.08 2.2 19.5 1 1 4 1## 7 26 4 120. 91 4.43 2.14 16.7 0 1 52 # # 8 27.3 4 79 66 4.08 1.94 18.9 1 14 1## 9 33.9 4 71.1 65 4.22 1.84 19.9 1 14 1## 10 30.4 4 75.7 52 4.93 1.62 18.5 1 1 4 2 # #... This is the end of with 22 more rows how to merge dplyr in R language. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report