簡體   English   中英

嵌套列表中的 inner_join 和 output 在 R 中的單個 dataframe

[英]inner_join within a nested list and output a single dataframe in R

我有一份摩托車各種技術規格的嵌套列表。 但是,規格只有 90% 相同。 某些列表中缺少某些規范,而另一些列表中存在某些規范。 我想要一個數據框,其中僅包含每個配對列表的第一列中存在的常見技術規格,並給出一個數據框的 output 用於比較自行車模型。

我在下面粘貼了一個示例數據。

list(Index = c("Model:", "Years:", "Engine:", "Capacity:", "Bore x Stroke:", 
"Cooling System:", "Compression Ratio:", "Lubrication:", "Induction:", 
"Ignition:", "Starting:", "Max Power:", "Max Torque:", "Clutch:", 
"Transmission:", "Final Drive:", "Primary Reduction Ratio:", 
"Gear Ratios:", "Final reduction ratio:", "Frame:", "Front Suspension:", 
"Front Wheel Travel:", "Rear Suspension:", "Rear Wheel Travel:", 
"Front Brakes:", "Rear Brakes:", "ABS:", "Front Tyre:", "Rear Tyre:", 
"Trail:", "Dimensions:", "Wheelbase:", "Seat Height:", "Ground Clearance:", 
"Wet Weight:", "Fuel Capacity:"), Value = c("Z 900", "2021", 
"Four stroke, transverse four cylinder, DOHC, 4 valves per cylinder", 
"948 cc / 57.8 cu-in", "73.4 x 56.0 mm", "Liquid cooled", "11.8:1", 
"Forced lubrication, wet sump", "Fuel injection: 36 mm x 4 with sub-throttle", 
"TCBI with electronic advance", "Electric", "92.2 kW / 123.6 hp @ 9500 rpm", 
"98.6 Nm / 72.7 lb-ft @ 7700 rpm", "Wet multi-disc, manual", 
"6 Speed", "Sealed chain", "1.627 (83/51)", "1st 2.692 (35/13) 2nd 2.059 (35/17) 3rd 1.650 (33/20) 4th 1.409 (31/22) 5th 1.222 (33/27) 6th 1.034 (30/29)", 
"2.933 (44/15)", "High-tensile steel trellis", "41 mm inverted fork with rebound damping and spring preload adjustability", 
"120 mm / 4.7 in", "Horizontal Back-link with rebound damping and spring preload adjustability", 
"140 mm / 5.5 in", "2x 300 mm semi-floating discs, dual opposed 4 piston calipers", 
"Single 250mm petal disc, 1 piston caliper", "Standard", "120/70ZR17M/C (58W)", 
"180/55ZR17M/C (73W)", "110 mm / 4.3 in", "Length 2070 mm / 81.4 in Width 825 mm / 32.4 in Height 1080 mm / 42.5 in", 
"1455.4 mm / 57.3 in", "820 mm / 32.2 in", "145 mm / 5.7 in", 
"212 kg / 467.5 lbs", "17 Litres / 4.5 US gal"), Index = c("Model:", 
"Years:", "Engine:", "Capacity:", "Bore x Stroke:", "Cooling System:", 
"Compression Ratio:", "Induction:", "Ignition:", "Starting:", 
"Max Power:", "Max Torque:", "Clutch:", "Transmission:", "Final Drive:", 
"Frame:", "Front Suspension:", "Front Wheel Travel:", "Rear Suspension:", 
"Rear Wheel Travel:", "Front Brakes:", "Rear Brakes:", "Rims:", 
"Front Wheel:", "Rear Wheel:", "Front Tyre:", "Rear Tyre:", "Rake:", 
"Trail:", "Dimensions:", "Wheelbase:", "Seat Height:", "Ground Clearance:", 
"Wet Weight:", "Oil Capacity:", "Fuel Capacity:", "Instruments:"
), Value = c("CBR 300R", "2020", "Four stroke, single cylinder, DOHC, 4 valve", 
"286 cc / 17.45 cub in.", "76 x 63 mm", "Liquid cooled", "10.7: 1", 
"PGM-FI electronic fuel injection", "Computer-controlled digital transistor with electronic advance", 
"Electric", "22.7 kW / 30.4 hp @ 8 500 rpm", "27 Nm / 2.75 kgf-m / 19.9 bl/ft @ 7 250 rpm", 
"Wet multiplate hydraulic clutch", "6 Speed", "#520 O-ring-sealed chain", 
"Diamond; steel twin-spar", "37 mm telescopic fork", "120 mm / 4.7 in", 
"Monoshock damper, Pro-Link swingarm, 5 position preload adjustability", 
"103 mm / 4.07 in", "Single 296 mm disc 2 piston caliper, ABS", 
"Single 220 mm disc 1 piston caliper", "Multi-spoke cast aluminium;", 
"MT2.75 x 17M/C", "MT4.00 x 17M/C", "110/70-17 radial", "140/70-17 radial", 
"25.3°", "98 mm / 3.9 in", "Length 2035 mm / 82.0 in Width 720 mm / 28.3 in Height 1120 mm / 44.0 in", 
"1380 mm / 54.3", "780 mm / 30.7 in", "145 mm / 5.7 in", "162 kg / 357 lbs (165 kg / 362 lbs ABS)", 
"1.9 Litres", "13 Litres / 3.4 gal", "Analogue tachometer, digital odometer, speedometer, fuel gauge, temperature gauge and clock."
), Index = c("Model:", "Years:", "Engine:", "Capacity:", "Bore x Stroke:", 
"Cooling System:", "Compression Ratio:", "Induction:", "Emission:", 
"Lubrication:", "Ignition:", "Starting:", "Max Power:", "Max Torque:", 
"Clutch:", "Transmission:", "Final Drive:", "Frame:", "Front Suspension:", 
"Front Wheel Travel:", "Rear Suspension:", "Rear Wheel Travel:", 
"Front Brakes:", "Rear Brakes:", "ABS:", "Front Tyre:", "Rear Tyre:", 
"Steering Head Angle:", "Seat Height:", "Wheelbase:", "Ground Clearance:", 
"Dry Weight:", "Fuel Capacity:"), Value = c("390 Duke", "2021", 
"Four stroke, single cylinder, DOHC with finger followers, 4 valves", 
"373 cc / 22.7 cu-in", "89 x 60 mm", "Liquid Cooled", "12.6:1", 
"Bosch Injection", "EURO5", "Wet sump", "Bosch EMS with RBW", 
"Electric", "43.5 hp / 32 kW @ 9500 rpm", "37 Nm / 27.2 lb-ft @ 7000rpm", 
"Wet multi-disc clutch, hydraulically actuated", "6 Speed", "Chain 520-X Ring", 
"Steel trellis frame, powder coated", "43mm WP APEX 43", "142 mm / 5.6 in", 
"WP APEX - Monoshock", "150 mm / 5.9 in", "Single 320 mm disc, 4 piston radial fixed caliper", 
"Single 230 mm disc, 1 piston floating caliper", "Bosch 9.1MP Two Channel ABS (Supermoto ABS)", 
"110/70 -17", "150/60-17", "65°", "830 mm / 32.7 in", "1357 mm / 53.4 in", 
"175 mm / 6.8 in", "149 kg / 328 lbs", "13.4 Litres / 3.5 US gal"
), Index = c("Model:", "Years:", "Engine:", "Capacity:", "Bore x Stroke:", 
"Cooling System:", "Compression Ratio:", "Exhaust:", "Emission control system:", 
"Lubrication:", "Induction:", "Engine Management:", "Starting:", 
"Max Power:", "Max Torque:", "Clutch:", "Transmission:", "Final Drive:", 
"Frame:", "Front Suspension:", "Front Wheel Travel:", "Rear Suspension:", 
"Rear Wheel Travel:", "Front Brakes:", "Rear Brakes:", "ABS:", 
"Wheels:", "Front Wheel:", "Rear Wheel:", "Front Tyre:", "Rear Tyre:", 
"Steering Head Angle:", "Rake:", "Wheelbase:", "Seat Height:", 
"Ground Clearance:", "Wet Weight:", "Fuel Capacity:"), Value = c("890 Adventure", 
"2021", "Four stroke, parallel twin cylinder, DOHC, 4 valves per cylinder", 
"889 cc / 54.3 cu-in", "90.7 x 68.8 mm", "Liquid cooled with water/oil heat exchanger", 
"13.5:1", "Stainless-steel", "Euro 5", "Pressure lubrication with 2 oil pumps", 
"DKK Dell’Orto with 46mm throttle body", "Bosch EMS with RBW", 
"Electric", "78.3 kW / 105 hp @ 8000 rpm", "100 Nm / 73.7 lb-ft @ 6500 rpm", 
"PASC antihopping clutch, mechanically operated", "6 Speed", 
"Chain 520 X-Ring", "Chromium-Molybdenum-Steel frame using the engine as stressed element, powder coated", 
"48mm WP APEX 43 forks", "200 mm / 7.87 in", "WP APEX - Monoshock", 
"200 mm / 7.87 in", "2x 320mm discs with 4-piston radially mounted calipers", 
"Single 260mm disc 2-piston floating caliper", "Bosch 9.1 MP incl. Cornering-ABS and offroad mode, disengageable", 
"Wire-spoked", "2.50 x 21", "4.50 x 18", "90/90 x 21 Continental Twinduro TKC 80", 
"150/70 x 18 Continental Twinduro TKC 80", "64.1°", "26.3°", 
"1529 mm / 60.2 in", "850 mm / 33.46 in", "233 mm / 9.17 in", 
"210 kg / 464 lbs", "20 Litres / 5.3 US gal"))

這是一種使用 tidyverse 的方法。 首先確定list_data的公共索引:

library(tidyverse)
common_indices = reduce(list_data[names(list_data) == "Index"], intersect)

接下來,我假設數據總是成對出現。 我的策略:

  1. 為每輛自行車創建一個數據框列表。
  2. 使用common_indices過濾這些數據幀。
  3. 使用purrr::reduce()將所有內容組合到一個表中。
  4. 使用tidyr::pivot_wider()使每輛自行車占據一排。
1:(length(list_data) / 2) %>% 
  # Step 1: Create a data frame for each bike
  map(function(i) {
    # The bike is located in the following indices of list_data
    bike_i = (2 * i- 1):(2 * i)
    list_data[bike_i] %>% 
      as_tibble() %>% 
      # Create an id variable as it will be used to identify the bike later
      mutate(id = i)
  }) %>% 
  # Step 2
  map(~ filter(.x, Index %in% common_indices)) %>% 
  # Step 3
  reduce(bind_rows) %>% 
  # Optional: clean the Index column.
  mutate(Index = str_remove(Index, ":$")) %>% 
  # Step 4
  pivot_wider(
    values_from = "Value", 
    names_from = "Index", 
    id_cols = id
  )

Output:

# A tibble: 4 x 28
     id Model  Years Engine    Capacity `Bore x Stroke` `Cooling System`
  <int> <chr>  <chr> <chr>     <chr>    <chr>           <chr>           
1     1 Z 900  2021  Four str~ 948 cc ~ 73.4 x 56.0 mm  Liquid cooled   
2     2 CBR 3~ 2020  Four str~ 286 cc ~ 76 x 63 mm      Liquid cooled   
3     3 390 D~ 2021  Four str~ 373 cc ~ 89 x 60 mm      Liquid Cooled   
4     4 890 A~ 2021  Four str~ 889 cc ~ 90.7 x 68.8 mm  Liquid cooled w~
# ... with 21 more variables: Compression Ratio <chr>, Induction <chr>,
#   Starting <chr>, Max Power <chr>, Max Torque <chr>, Clutch <chr>,
#   Transmission <chr>, Final Drive <chr>, Frame <chr>,
#   Front Suspension <chr>, Front Wheel Travel <chr>,
#   Rear Suspension <chr>, Rear Wheel Travel <chr>, Front Brakes <chr>,
#   Rear Brakes <chr>, Front Tyre <chr>, Rear Tyre <chr>,
#   Wheelbase <chr>, Seat Height <chr>, Ground Clearance <chr>,
#   Fuel Capacity <chr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM