如何在dplyr中使用`bind_rows`处理大小不兼容的列表?

huangapple go评论104阅读模式
英文:

How to use bind_rows for a list with incompatible sizes in dplyr?

问题

I have translated the code part as requested. Here is the translated code:

  1. library(tidyverse)
  2. df %>%
  3. purrr::transpose() %>%
  4. `$`(.borders) %>%
  5. bind_rows() %>%
  6. pivot_longer(everything(), values_to = "Borders")

Please let me know if you need any further assistance.

英文:

I am having a data like the following

  1. $ECe
  2. .type .value .borders .feature
  3. 1 ale -0.866 0.490 ECe
  4. 2 ale -0.811 2.680 ECe
  5. 3 ale -0.749 4.990 ECe
  6. 4 ale -0.501 7.994 ECe
  7. 5 ale -0.472 8.801 ECe
  8. 6 ale -0.364 11.140 ECe
  9. 7 ale -0.351 14.090 ECe
  10. 8 ale -0.172 15.900 ECe
  11. 9 ale -0.068 17.810 ECe
  12. 10 ale 0.069 18.750 ECe
  13. 11 ale 0.132 20.190 ECe
  14. 12 ale 0.176 21.020 ECe
  15. 13 ale 0.227 22.950 ECe
  16. 14 ale 0.295 24.520 ECe
  17. 15 ale 0.458 28.290 ECe
  18. 16 ale 0.534 31.640 ECe
  19. 17 ale 0.265 37.990 ECe
  20. 18 ale 0.330 40.700 ECe
  21. 19 ale 0.793 45.117 ECe
  22. 20 ale 0.393 53.090 ECe
  23. 21 ale 0.752 59.719 ECe
  24. $BD
  25. .type .value .borders .feature
  26. 1 ale 2.88401670 0.957 BD
  27. 2 ale 2.89757908 1.138 BD
  28. 3 ale 2.06229728 1.185 BD
  29. 4 ale 1.86256683 1.220 BD
  30. 5 ale 1.22004089 1.299 BD
  31. 6 ale 0.47244692 1.339 BD
  32. 7 ale 0.28531498 1.401 BD
  33. 8 ale 0.21050138 1.486 BD
  34. 9 ale 0.06942624 1.500 BD
  35. 10 ale -0.01446751 1.524 BD
  36. 11 ale -0.92647637 1.637 BD
  37. 12 ale -0.99773176 1.651 BD
  38. 13 ale -1.62164363 1.704 BD
  39. 14 ale -2.12120182 1.800 BD
  40. 15 ale -1.87179280 1.983 BD
  41. 16 ale -1.64798056 2.075 BD
  42. 17 ale -1.55269957 2.170 BD
  43. 18 ale -1.64049346 2.220 BD

I want to create a data frame like

  1. # A tibble: 120 × 2
  2. name Borders
  3. <chr> <dbl>
  4. 1 ECe 0.491
  5. 2 SOC 0.261
  6. 3 BD 0.958
  7. 4 Sand 4.00
  8. 5 Silt 0
  9. 6 Clay 0
  10. 7 ECe 3.61
  11. 8 SOC 0.387
  12. 9 BD 1.02
  13. 10 Sand 8.42
  14. # ℹ 110 more rows

I am using the following code

  1. library(tidyverse)
  2. df %>%
  3. purrr::transpose() %>%
  4. `$`(.borders) %>%
  5. bind_rows() %>%
  6. pivot_longer(everything(), values_to = "Borders")

But it returns the following error
>Error in recycle_columns():
! Tibble columns must have compatible sizes.
• Size 18: Columns BD and Clay.
• Size 20: Columns Sand and Silt.
• Size 21: Columns ECe and SOC.
ℹ Only values of size one are recycled.

How can I achieve this in R?
Here is the data in dput format

  1. df <-
  2. list(
  3. ECe = structure(
  4. list(
  5. .type = c(
  6. "ale",
  7. "ale",
  8. "ale",
  9. "ale",
  10. "ale",
  11. "ale",
  12. "ale",
  13. "ale",
  14. "ale",
  15. "ale",
  16. "ale",
  17. "ale",
  18. "ale",
  19. "ale",
  20. "ale",
  21. "ale",
  22. "ale",
  23. "ale",
  24. "ale",
  25. "ale",
  26. "ale"
  27. ),
  28. .value = c(
  29. -0.866449860261058,-0.811874082709701,
  30. -0.749993003099365,
  31. -0.501212768536108,
  32. -0.47268510237864,-0.364538974363897,
  33. -0.351930853130633,
  34. -0.172990179989361,
  35. -0.0684651440322241,
  36. 0.0692627161333526,
  37. 0.132807737313057,
  38. 0.176914094922519,
  39. 0.227473041741352,
  40. 0.295586935903242,
  41. 0.458768756260148,
  42. 0.534728976733407,
  43. 0.265760631807008,
  44. 0.330347944699875,
  45. 0.793585231260766,
  46. 0.393724931630266,
  47. 0.752824545868792
  48. ),
  49. .borders = c(
  50. 0.490806,
  51. 2.68,
  52. 4.99,
  53. 7.994,
  54. 8.801,
  55. 11.14,
  56. 14.09,
  57. 15.9,
  58. 17.81,
  59. 18.75,
  60. 20.19,
  61. 21.02,
  62. 22.95,
  63. 24.52,
  64. 28.29,
  65. 31.64,
  66. 37.99,
  67. 40.7,
  68. 45.1172,
  69. 53.09,
  70. 59.7192
  71. ),
  72. .feature = c(
  73. "ECe",
  74. "ECe",
  75. "ECe",
  76. "ECe",
  77. "ECe",
  78. "ECe",
  79. "ECe",
  80. "ECe",
  81. "ECe",
  82. "ECe",
  83. "ECe",
  84. "ECe",
  85. "ECe",
  86. "ECe",
  87. "ECe",
  88. "ECe",
  89. "ECe",
  90. "ECe",
  91. "ECe",
  92. "ECe",
  93. "ECe"
  94. )
  95. ),
  96. row.names = c(NA,-21L),
  97. class = "data.frame"
  98. ),
  99. SOC = structure(
  100. list(
  101. .type = c(
  102. "ale",
  103. "ale",
  104. "ale",
  105. "ale",
  106. "ale",
  107. "ale",
  108. "ale",
  109. "ale",
  110. "ale",
  111. "ale",
  112. "ale",
  113. "ale",
  114. "ale",
  115. "ale",
  116. "ale",
  117. "ale",
  118. "ale",
  119. "ale",
  120. "ale",
  121. "ale",
  122. "ale"
  123. ),
  124. .value = c(
  125. -1.12668195657973,-1.14070505975704,
  126. -1.12096557264514,
  127. -1.0364427676548,
  128. -0.905420835797799,-0.683789433176536,
  129. -0.53425868958447,
  130. -0.483272349572241,-0.360467057150275,
  131. -0.0571214846376487,
  132. 0.0130773857269451,
  133. 0.164166766380452,
  134. 0.233741319768938,
  135. 0.174784707378661,
  136. 0.188264622491949,
  137. 0.150568432447101,
  138. 0.532302724503294,
  139. 0.733931793684184,
  140. 1.1531117354405,
  141. 2.58573209158873,
  142. 2.75830864578748
  143. ),
  144. .borders = c(
  145. 0.260869565217391,
  146. 0.330143540669856,
  147. 0.422727272727273,
  148. 0.545454545454545,
  149. 0.631578947368421,
  150. 0.686915887850467,
  151. 0.763636363636364,
  152. 0.802631578947368,
  153. 0.828571428571429,
  154. 0.889952153110048,
  155. 0.967289719626168,
  156. 1.02272727272727,
  157. 1.09090909090909,
  158. 1.23684210526316,
  159. 1.40520446096654,
  160. 1.52727272727273,
  161. 1.80861244019139,
  162. 1.90909090909091,
  163. 2.20093457943925,
  164. 2.49767441860465,
  165. 2.66511627906977
  166. ),
  167. .feature = c(
  168. "SOC",
  169. "SOC",
  170. "SOC",
  171. "SOC",
  172. "SOC",
  173. "SOC",
  174. "SOC",
  175. "SOC",
  176. "SOC",
  177. "SOC",
  178. "SOC",
  179. "SOC",
  180. "SOC",
  181. "SOC",
  182. "SOC",
  183. "SOC",
  184. "SOC",
  185. "SOC",
  186. "SOC",
  187. "SOC",
  188. "SOC"
  189. )
  190. ),
  191. row.names = c(NA,-21L),
  192. class = "data.frame"
  193. ),
  194. BD = structure(
  195. list(
  196. .type = c(
  197. "ale",
  198. "ale",
  199. "ale",
  200. "ale",
  201. "ale",
  202. "ale",
  203. "ale",
  204. "ale",
  205. "ale",
  206. "ale",
  207. "ale",
  208. "ale",
  209. "ale",
  210. "ale",
  211. "ale",
  212. "ale",
  213. "ale",
  214. "ale"
  215. ),
  216. .value = c(
  217. 2.88401670431712,
  218. 2.89757908472024,
  219. 2.06229728227612,
  220. 1.8625668296819,
  221. 1.22004089065561,
  222. 0.472446924052171,
  223. 0.285314983167609,
  224. 0.210501378242261,
  225. 0.0694262373804251,-0.0144675088601809,
  226. -0.926476372785716,
  227. -0.997731763217841,-1.62164362525624,
  228. -2.12120181627754,
  229. -1.87179280480865,
  230. -1.64798056454827,-1.5526995652836,
  231. -1.64049345720051
  232. ),
  233. .borders = c(
  234. 0.957710151765353,
  235. 1.13854777070064,
  236. 1.18522292993631,
  237. 1.22058598726115,
  238. 1.29930014940631,
  239. 1.33971974522293,
  240. 1.40121097743178,
  241. 1.48638829912715,
  242. 1.5,
  243. 1.52428025477707,
  244. 1.6371974522293,
  245. 1.65183612487222,
  246. 1.70405095541401,
  247. 1.80025477707006,
  248. 1.98361248722183,
  249. 2.07583549579303,
  250. 2.17044900526854,
  251. 2.22039789258473
  252. ),
  253. .feature = c(
  254. "BD",
  255. "BD",
  256. "BD",
  257. "BD",
  258. "BD",
  259. "BD",
  260. "BD",
  261. "BD",
  262. "BD",
  263. "BD",
  264. "BD",
  265. "BD",
  266. "BD",
  267. "BD",
  268. "BD",
  269. "BD",
  270. "BD",
  271. "BD"
  272. )
  273. ),
  274. row.names = c(NA,-18L),
  275. class = "data.frame"
  276. ),
  277. Sand = structure(
  278. list(
  279. .type = c(
  280. "ale",
  281. "ale",
  282. "ale",
  283. "ale",
  284. "ale",
  285. "ale",
  286. "ale",
  287. "ale",
  288. "ale",
  289. "ale",
  290. "ale",
  291. "ale",
  292. "ale",
  293. "ale",
  294. "ale",
  295. "ale",
  296. "ale",
  297. "ale",
  298. "ale",
  299. "ale"
  300. ),
  301. .value = c(
  302. 5.72243888922004,
  303. 5.04938965197184,
  304. 4.7017193997508,
  305. 4.38186986390524,
  306. 3.55049136618825,
  307. 3.79907192663079,
  308. 3.65192607277815,
  309. 2.69221076986002,
  310. 2.08417035244566,
  311. 0.508134498143654,
  312. -0.760157331236183,-2.22196153094718,
  313. -2.60183427497522,
  314. -2.6090089814882,
  315. -4.29829842975557,-4.89113681989785,
  316. -6.17719615317024,
  317. -6.51041335854504,
  318. -6.70721878970499,-6.69985492661679
  319. ),
  320. .borders = c(
  321. 3.9999999999992,
  322. 7.99999999999983,
  323. 12.0000000000005,
  324. 20.0000000000003,
  325. 23.9999999999995,
  326. 27.9999999999987,
  327. 31.9999999999993,
  328. 39.9999999999991,
  329. 43.9999999999998,
  330. 48.0000000000004,
  331. 52.000000000001,
  332. 59.9999999999994,
  333. 63.9999999999986,
  334. 64.0000000000001,
  335. 68.0000000000007,
  336. 71.9999999999999,
  337. 76.0000000000005,
  338. 79.9999999999997,
  339. 84.0000000000003,
  340. 88.000000000001
  341. ),
  342. .feature = c(
  343. "Sand",
  344. "Sand",
  345. "Sand",
  346. "Sand",
  347. "Sand",
  348. "Sand",
  349. "Sand",
  350. "Sand",
  351. "Sand",
  352. "Sand",
  353. "Sand",
  354. "Sand",
  355. "Sand",
  356. "Sand",
  357. "Sand",
  358. "Sand",
  359. "Sand",
  360. "Sand",
  361. "Sand",
  362. "Sand"
  363. )
  364. ),
  365. row.names = c(NA,-20L),
  366. class = "data.frame"
  367. ),
  368. Silt = structure(
  369. list(
  370. .type = c(
  371. "ale",
  372. "ale",
  373. "ale",
  374. "ale",
  375. "ale",
  376. "ale",
  377. "ale",
  378. "ale",
  379. "ale",
  380. "ale",
  381. "ale",
  382. "ale",
  383. "ale",
  384. "ale",
  385. "ale",
  386. "ale",
  387. "ale",
  388. "ale",
  389. "ale",
  390. "ale"
  391. ),
  392. .value = c(
  393. -1.81467365524819,-1.80595765781436,
  394. -1.44252832794551,
  395. -1.40678289959526,-1.3809409627197,
  396. -1.27631650711468,
  397. -0.918535212874314,-0.891479949841867,
  398. -0.130663280250859,
  399. 0.054226944600215,
  400. 0.0371390649359506,
  401. 0.547567627523539,
  402. 0.540716330465325,
  403. 1.14691203242341,
  404. 1.45243650519454,
  405. 1.51824963792443,
  406. 1.76292774269082,
  407. 1.85725359158924,
  408. 2.48581029669555,
  409. 0.388409658807059
  410. ),
  411. .borders = c(
  412. 0,
  413. 3.99999999999921,
  414. 7.99999999999841,
  415. 7.99999999999983,
  416. 8.00000000000125,
  417. 11.999999999999,
  418. 15.9999999999997,
  419. 16.0000000000011,
  420. 20.0000000000003,
  421. 23.9999999999995,
  422. 24.0000000000009,
  423. 27.9999999999987,
  424. 28.0000000000001,
  425. 31.9999999999993,
  426. 35.9999999999985,
  427. 40.0000000000006,
  428. 43.9999999999998,
  429. 47.999999999999,
  430. 64.0000000000015,
  431. 79.9999999999997
  432. ),
  433. .feature = c(
  434. "Silt",
  435. "Silt",
  436. "Silt",
  437. "Silt",
  438. "Silt",
  439. "Silt",
  440. "Silt",
  441. "Silt",
  442. "Silt",
  443. "Silt",
  444. "Silt",
  445. "Silt",
  446. "Silt",
  447. "Silt",
  448. "Silt",
  449. "Silt",
  450. "Silt",
  451. "Silt",
  452. "Silt",
  453. "Silt"
  454. )
  455. ),
  456. row.names = c(NA,-20L),
  457. class = "data.frame"
  458. ),
  459. Clay = structure(
  460. list(
  461. .type = c(
  462. "ale",
  463. "ale",
  464. "ale",
  465. "ale",
  466. "ale",
  467. "ale",
  468. "ale",
  469. "ale",
  470. "ale",
  471. "ale",
  472. "ale",
  473. "ale",
  474. "ale",
  475. "ale",
  476. "ale",
  477. "ale",
  478. "ale",
  479. "ale"
  480. ),
  481. .value = c(
  482. -1.02199413478959,-0.826304780697195,
  483. -0.942406575344331,
  484. -0.971009582383038,-1.24981505440423,
  485. -1.24219672780758,
  486. -0.443954661148336,-0.344646636267013,
  487. -0.344646636267013,
  488. 0.0190677799370838,
  489. 0.509523035852588,
  490. 0.805292224788913,
  491. 0.990611614682741,
  492. 0.998147256094054,
  493. 1.16179870265779,
  494. 2.06234942853985,
  495. 2.8900928986798,
  496. 3.82397972588229
  497. ),
  498. .borders = c(
  499. 0,
  500. 7.99999999999983,
  501. 11.999999999999,
  502. 12.0000000000005,
  503. 15.9999999999997,
  504. 16.0000000000011,
  505. 20.0000000000003,
  506. 23.9999999999995,
  507. 24.0000000000009,
  508. 28.0000000000001,
  509. 31.9999999999993,
  510. 35.9999999999999,
  511. 39.9999999999991,
  512. 40.0000000000006,
  513. 47.999999999999,
  514. 51.9999999999996,
  515. 56.0000000000002,
  516. 67.9999999999993
  517. ),
  518. .feature = c(
  519. "Clay",
  520. "Clay",
  521. "Clay",
  522. "Clay",
  523. "Clay",
  524. "Clay",
  525. "Clay",
  526. "Clay",
  527. "Clay",
  528. "Clay",
  529. "Clay",
  530. "Clay",
  531. "Clay",
  532. "Clay",
  533. "Clay",
  534. "Clay",
  535. "Clay",
  536. "Clay"
  537. )
  538. ),
  539. row.names = c(NA,-18L),
  540. class = "data.frame"
  541. )
  542. )

答案1

得分: 2

以下是翻译好的内容:

使用 map()list_rbind()

  1. library(tidyverse)
  2. map(df, ~ select(.x, Name = .feature, Borders = .borders, Value = .value)) %>%
  3. list_rbind() %>%
  4. group_by(Name) %>%
  5. mutate(row_order = 1:n()) %>%
  6. ungroup() %>%
  7. arrange(row_order) %>%
  8. select(-row_order)
  1. # 一个数据框:118 行 × 3 列
  2. Name Borders Value
  3. <chr> <dbl> <dbl>
  4. 1 ECe 0.491 -0.866
  5. 2 SOC 0.261 -1.13
  6. 3 BD 0.958 2.88
  7. 4 Sand 4.00 5.72
  8. 5 Silt 0 -1.81
  9. 6 Clay 0 -1.02
  10. 7 ECe 2.68 -0.812
  11. 8 SOC 0.330 -1.14
  12. 9 BD 1.14 2.90
  13. 10 Sand 8.00 5.05
  14. # … 还有 108 行

如果顺序不重要,您可以使用以下方式:

  1. map(df, ~ select(.x, Name = .feature, Borders = .borders, Value = .value)) %>%
  2. list_rbind()
英文:

Use map() and list_rbind():

  1. library(tidyverse)
  2. map(df, ~ select(.x, Name = .feature, Borders = .borders, Value = .value)) |&gt;
  3. list_rbind() |&gt;
  4. group_by(Name) |&gt;
  5. mutate(row_order = 1:n()) |&gt;
  6. ungroup() |&gt;
  7. arrange(row_order) |&gt;
  8. select(-row_order)
  1. # A tibble: 118 &#215; 3
  2. Name Borders Value
  3. &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  4. 1 ECe 0.491 -0.866
  5. 2 SOC 0.261 -1.13
  6. 3 BD 0.958 2.88
  7. 4 Sand 4.00 5.72
  8. 5 Silt 0 -1.81
  9. 6 Clay 0 -1.02
  10. 7 ECe 2.68 -0.812
  11. 8 SOC 0.330 -1.14
  12. 9 BD 1.14 2.90
  13. 10 Sand 8.00 5.05
  14. # … with 108 more rows

If the order does not matter, you can just go with:

  1. map(df, ~ select(.x, Name = .feature, Borders = .borders, Value = .value)) |&gt;
  2. list_rbind()

答案2

得分: 2

Here is the translated content you requested:

Udpate OP请求见评论:

要添加.value列:

  1. imap_dfr(df, ~ data.frame(name = .y, Borders = .x$.borders, value = .x$.value))
  2. name Borders value
  3. <chr> <dbl> <dbl>
  4. 1 ECe 0.491 -0.866
  5. 2 ECe 2.68 -0.812
  6. 3 ECe 4.99 -0.750
  7. 4 ECe 7.99 -0.501
  8. 5 ECe 8.80 -0.473
  9. 6 ECe 11.1 -0.365
  10. 7 ECe 14.1 -0.352
  11. 8 ECe 15.9 -0.173
  12. 9 ECe 17.8 -0.0685
  13. 10 ECe 18.8 0.0693
  14. # … with 108 more rows

第一个答案
为了解决您遇到的错误:

  1. 使用 purrr::transpose() %>% $(.borders) 生成了不同长度的6个向量的列表。
    然后您尝试将它们按列与bind_rows组合到一个tibble中。错误消息显示:
  1. Size 18: Columns `BD` and `Clay`.
  2. Size 20: Columns `Sand` and `Silt`.
  3. Size 21: Columns `ECe` and `SOC`.
  4. Only values of size one are recycled.```
  5. 要实现您想要的操作,您可以使用 `purrr::map_df()` 或其变体 `purrr::imap_dfr()`:
  6. 这是使用 `map_df` 的版本:
  7. ```R
  8. library(purrr)
  9. library(dplyr)
  10. map(df, ~(.x %>% select(c(.feature, .borders)))) %>%
  11. map_df(., ~ .x %>% select(.borders), .id = "name")

或者

  1. library(purrr)
  2. library(dplyr)
  3. imap_dfr(df, ~ data.frame(name = .y, Borders = .x$.borders))
  1. name .borders
  2. 1 ECe 0.4908060
  3. 2 ECe 2.6800000
  4. 3 ECe 4.9900000
  5. 4 ECe 7.9940000
  6. 5 ECe 8.8010000
  7. 6 ECe 11.1400000
  8. 7 ECe 14.0900000
  9. 8 ECe 15.9000000
  10. 9 ECe 17.8100000
  11. 10 ECe 18.7500000
  12. 11 ECe 20.1900000
  13. 12 ECe 21.0200000
  14. 13 ECe 22.9500000
  15. 14 ECe 24.5200000
  16. 15 ECe 28.2900000
  17. 16 ECe 31.6400000
  18. 17 ECe 37.9900000
  19. 18 ECe 40.7000000
  20. 19 ECe 45.1172000
  21. 20 ECe 53.0900000
  22. 21 ECe 59.7192000
  23. 22 SOC 0.2608696
  24. 23 SOC 0.3301435
  25. 24 SOC 0.4227273
  26. 25 SOC 0.5454545
  27. 26 SOC 0.6315789
  28. 27 SOC 0.6869159
  29. 28 SOC 0.7636364
  30. 29 SOC 0.8026316
  31. 30 SOC 0.8285714
  32. 31 SOC 0.8899522
  33. 32 SOC 0.9672897
  34. 33 SOC 1.0227273
  35. 34 SOC 1.0909091
  36. 35 SOC 1.2368421
  37. 36 SOC 1.4052045
  38. 37 SOC 1.5272727
  39. 38 SOC 1.8086124
  40. 39 SOC 1.9090909
  41. 40 SOC 2.2009346
  42. 41 SOC 2.4976744
  43. 42 SOC 2.6651163
  44. 43 BD 0.9577102
  45. 44 BD 1.1385478
  46. 45 BD 1.1852229
  47. 46 BD 1.2205860
  48. 47 BD 1.2993001
  49. 48 BD 1.3397197
  50. 49 BD 1.4012110
  51. 50 BD 1.4863883
  52. 51 BD 1.5000000
  53. 52 BD 1.5242803
  54. 53 BD 1.6371975
  55. 54 BD 1.6518361
  56. 55 BD 1.7040510
  57. 56 BD 1.8002548
  58. 57 BD 1.9836125
  59. 58 BD 2.0758355
  60. 59 BD 2.1704490
  61. 60 BD 2.2203979
  62. 61 Sand 4.0000000
  63. 62 Sand 8.0000000
  64. 63 Sand 12.0000000
  65. 64 Sand 20.0000000
  66. 65 Sand 24.0000000
  67. 66 Sand 28.0000000
  68. 67 Sand 32.0000000
  69. 68 Sand 40.0000000
  70. 69 Sand 44.0000000
  71. 70 Sand 48.0000000
  72. 71 Sand 52.0000000
  73. 72 Sand 60.0000000
  74. 73 Sand 64.0000000
  75. 74 Sand 64.0000000
  76. 75 Sand 68.0000000
  77. 76 Sand 72.0000000
  78. 77 Sand 76.0000000
  79. 78 Sand 80.0000000
  80. 79 Sand 84.0000000
  81. 80 Sand 88.0000000
  82. 81 Silt 0.0000000
  83. 82 Silt 4.0000000
  84. 83 Silt 8.0000000
  85. 84 Silt 8.0000000
  86. 85 Silt 8.0000000
  87. 86 Silt 12.0000000
  88. 87 Silt 16.0000000
  89. 88 Silt 16.0000000
  90. 89 Silt 20.0000000
  91. 90 Silt 24.0000000
  92. 91 Silt 24.0000000
  93. 92 Silt 28.0000000
  94. 93 Silt 28.000000
  95. <details>
  96. <summary>英文:</summary>
  97. **Udpate** OP request see comments:
  98. to add .value column:

imap_dfr(df, ~ data.frame(name = .y, Borders = .x$.borders, value = .x$.value))

name Borders value
<chr> <dbl> <dbl>
1 ECe 0.491 -0.866
2 ECe 2.68 -0.812
3 ECe 4.99 -0.750
4 ECe 7.99 -0.501
5 ECe 8.80 -0.473
6 ECe 11.1 -0.365
7 ECe 14.1 -0.352
8 ECe 15.9 -0.173
9 ECe 17.8 -0.0685
10 ECe 18.8 0.0693

… with 108 more rows

  1. **First answer:**
  2. To address the error you get:
  3. 1. With `purrr::transpose() %&gt;%
  4. `$`(.borders)` you create list of 6 vectors of different length.
  5. And then you try to combine them columnwise with bind_rows to a tibble. And the error says
  6. ``` ! Tibble columns must have compatible sizes.
  7. • Size 18: Columns `BD` and `Clay`.
  8. • Size 20: Columns `Sand` and `Silt`.
  9. • Size 21: Columns `ECe` and `SOC`.
  10. ℹ Only values of size one are recycled.```

To get what you want to do, you could either use purrr::map_df() or its variant purrr::imap_dfr():

Here is a version using map_df:

  1. library(purrr)
  2. library(dplyr)
  3. map(df, ~(.x %&gt;% select(c(.feature, .borders)))) %&gt;%
  4. map_df(., ~ .x %&gt;% select(.borders), .id = &quot;name&quot;)

OR

  1. library(purrr)
  2. library(dplyr)
  3. imap_dfr(df, ~ data.frame(name = .y, Borders = .x$.borders))
  1. name .borders
  2. 1 ECe 0.4908060
  3. 2 ECe 2.6800000
  4. 3 ECe 4.9900000
  5. 4 ECe 7.9940000
  6. 5 ECe 8.8010000
  7. 6 ECe 11.1400000
  8. 7 ECe 14.0900000
  9. 8 ECe 15.9000000
  10. 9 ECe 17.8100000
  11. 10 ECe 18.7500000
  12. 11 ECe 20.1900000
  13. 12 ECe 21.0200000
  14. 13 ECe 22.9500000
  15. 14 ECe 24.5200000
  16. 15 ECe 28.2900000
  17. 16 ECe 31.6400000
  18. 17 ECe 37.9900000
  19. 18 ECe 40.7000000
  20. 19 ECe 45.1172000
  21. 20 ECe 53.0900000
  22. 21 ECe 59.7192000
  23. 22 SOC 0.2608696
  24. 23 SOC 0.3301435
  25. 24 SOC 0.4227273
  26. 25 SOC 0.5454545
  27. 26 SOC 0.6315789
  28. 27 SOC 0.6869159
  29. 28 SOC 0.7636364
  30. 29 SOC 0.8026316
  31. 30 SOC 0.8285714
  32. 31 SOC 0.8899522
  33. 32 SOC 0.9672897
  34. 33 SOC 1.0227273
  35. 34 SOC 1.0909091
  36. 35 SOC 1.2368421
  37. 36 SOC 1.4052045
  38. 37 SOC 1.5272727
  39. 38 SOC 1.8086124
  40. 39 SOC 1.9090909
  41. 40 SOC 2.2009346
  42. 41 SOC 2.4976744
  43. 42 SOC 2.6651163
  44. 43 BD 0.9577102
  45. 44 BD 1.1385478
  46. 45 BD 1.1852229
  47. 46 BD 1.2205860
  48. 47 BD 1.2993001
  49. 48 BD 1.3397197
  50. 49 BD 1.4012110
  51. 50 BD 1.4863883
  52. 51 BD 1.5000000
  53. 52 BD 1.5242803
  54. 53 BD 1.6371975
  55. 54 BD 1.6518361
  56. 55 BD 1.7040510
  57. 56 BD 1.8002548
  58. 57 BD 1.9836125
  59. 58 BD 2.0758355
  60. 59 BD 2.1704490
  61. 60 BD 2.2203979
  62. 61 Sand 4.0000000
  63. 62 Sand 8.0000000
  64. 63 Sand 12.0000000
  65. 64 Sand 20.0000000
  66. 65 Sand 24.0000000
  67. 66 Sand 28.0000000
  68. 67 Sand 32.0000000
  69. 68 Sand 40.0000000
  70. 69 Sand 44.0000000
  71. 70 Sand 48.0000000
  72. 71 Sand 52.0000000
  73. 72 Sand 60.0000000
  74. 73 Sand 64.0000000
  75. 74 Sand 64.0000000
  76. 75 Sand 68.0000000
  77. 76 Sand 72.0000000
  78. 77 Sand 76.0000000
  79. 78 Sand 80.0000000
  80. 79 Sand 84.0000000
  81. 80 Sand 88.0000000
  82. 81 Silt 0.0000000
  83. 82 Silt 4.0000000
  84. 83 Silt 8.0000000
  85. 84 Silt 8.0000000
  86. 85 Silt 8.0000000
  87. 86 Silt 12.0000000
  88. 87 Silt 16.0000000
  89. 88 Silt 16.0000000
  90. 89 Silt 20.0000000
  91. 90 Silt 24.0000000
  92. 91 Silt 24.0000000
  93. 92 Silt 28.0000000
  94. 93 Silt 28.0000000
  95. 94 Silt 32.0000000
  96. 95 Silt 36.0000000
  97. 96 Silt 40.0000000
  98. 97 Silt 44.0000000
  99. 98 Silt 48.0000000
  100. 99 Silt 64.0000000
  101. 100 Silt 80.0000000
  102. 101 Clay 0.0000000
  103. 102 Clay 8.0000000
  104. 103 Clay 12.0000000
  105. 104 Clay 12.0000000
  106. 105 Clay 16.0000000
  107. 106 Clay 16.0000000
  108. 107 Clay 20.0000000
  109. 108 Clay 24.0000000
  110. 109 Clay 24.0000000
  111. 110 Clay 28.0000000
  112. 111 Clay 32.0000000
  113. 112 Clay 36.0000000
  114. 113 Clay 40.0000000
  115. 114 Clay 40.0000000
  116. 115 Clay 48.0000000
  117. 116 Clay 52.0000000
  118. 117 Clay 56.0000000
  119. 118 Clay 68.0000000

huangapple
  • 本文由 发表于 2023年5月14日 17:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定