我有一个数据如下
$ECe
.type .value .borders .feature
1 ale -0.866 0.490 ECe
2 ale -0.811 2.680 ECe
3 ale -0.749 4.990 ECe
4 ale -0.501 7.994 ECe
5 ale -0.472 8.801 ECe
6 ale -0.364 11.140 ECe
7 ale -0.351 14.090 ECe
8 ale -0.172 15.900 ECe
9 ale -0.068 17.810 ECe
10 ale 0.069 18.750 ECe
11 ale 0.132 20.190 ECe
12 ale 0.176 21.020 ECe
13 ale 0.227 22.950 ECe
14 ale 0.295 24.520 ECe
15 ale 0.458 28.290 ECe
16 ale 0.534 31.640 ECe
17 ale 0.265 37.990 ECe
18 ale 0.330 40.700 ECe
19 ale 0.793 45.117 ECe
20 ale 0.393 53.090 ECe
21 ale 0.752 59.719 ECe
$BD
.type .value .borders .feature
1 ale 2.88401670 0.957 BD
2 ale 2.89757908 1.138 BD
3 ale 2.06229728 1.185 BD
4 ale 1.86256683 1.220 BD
5 ale 1.22004089 1.299 BD
6 ale 0.47244692 1.339 BD
7 ale 0.28531498 1.401 BD
8 ale 0.21050138 1.486 BD
9 ale 0.06942624 1.500 BD
10 ale -0.01446751 1.524 BD
11 ale -0.92647637 1.637 BD
12 ale -0.99773176 1.651 BD
13 ale -1.62164363 1.704 BD
14 ale -2.12120182 1.800 BD
15 ale -1.87179280 1.983 BD
16 ale -1.64798056 2.075 BD
17 ale -1.55269957 2.170 BD
18 ale -1.64049346 2.220 BD
我想创建一个数据框,如
# A tibble: 120 × 2
name Borders
<chr> <dbl>
1 ECe 0.491
2 SOC 0.261
3 BD 0.958
4 Sand 4.00
5 Silt 0
6 Clay 0
7 ECe 3.61
8 SOC 0.387
9 BD 1.02
10 Sand 8.42
# ℹ 110 more rows
我正在使用以下代码
library(tidyverse)
df %>%
purrr::transpose() %>%
`$`(.borders) %>%
bind_rows() %>%
pivot_longer(everything(), values_to = "Borders")
但它返回以下错误recycle_columns()
中的错误:!Tibble列必须具有兼容的大小。·大小18:列xlmlnlx和xlm 2nlx。·大小20:列xlm 3 nlx和xlm 4 nlx。·大小21:列xlm 5 nlx和xlm 6 nlx。仅回收大小为1的值。
如何在R中实现这一点?以下是dput
的数据
df <- list(ECe = structure(list(.type = c("ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale"), .value = c(-0.866449860261058,
-0.811874082709701, -0.749993003099365, -0.501212768536108, -0.47268510237864,
-0.364538974363897, -0.351930853130633, -0.172990179989361, -0.0684651440322241,
0.0692627161333526, 0.132807737313057, 0.176914094922519, 0.227473041741352,
0.295586935903242, 0.458768756260148, 0.534728976733407, 0.265760631807008,
0.330347944699875, 0.793585231260766, 0.393724931630266, 0.752824545868792
), .borders = c(0.490806, 2.68, 4.99, 7.994, 8.801, 11.14, 14.09,
15.9, 17.81, 18.75, 20.19, 21.02, 22.95, 24.52, 28.29, 31.64,
37.99, 40.7, 45.1172, 53.09, 59.7192), .feature = c("ECe", "ECe",
"ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe",
"ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe", "ECe",
"ECe")), row.names = c(NA, -21L), class = "data.frame"), SOC = structure(list(
.type = c("ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale"), .value = c(-1.12668195657973,
-1.14070505975704, -1.12096557264514, -1.0364427676548, -0.905420835797799,
-0.683789433176536, -0.53425868958447, -0.483272349572241,
-0.360467057150275, -0.0571214846376487, 0.0130773857269451,
0.164166766380452, 0.233741319768938, 0.174784707378661,
0.188264622491949, 0.150568432447101, 0.532302724503294,
0.733931793684184, 1.1531117354405, 2.58573209158873, 2.75830864578748
), .borders = c(0.260869565217391, 0.330143540669856, 0.422727272727273,
0.545454545454545, 0.631578947368421, 0.686915887850467,
0.763636363636364, 0.802631578947368, 0.828571428571429,
0.889952153110048, 0.967289719626168, 1.02272727272727, 1.09090909090909,
1.23684210526316, 1.40520446096654, 1.52727272727273, 1.80861244019139,
1.90909090909091, 2.20093457943925, 2.49767441860465, 2.66511627906977
), .feature = c("SOC", "SOC", "SOC", "SOC", "SOC", "SOC",
"SOC", "SOC", "SOC", "SOC", "SOC", "SOC", "SOC", "SOC", "SOC",
"SOC", "SOC", "SOC", "SOC", "SOC", "SOC")), row.names = c(NA,
-21L), class = "data.frame"), BD = structure(list(.type = c("ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale"), .value = c(2.88401670431712,
2.89757908472024, 2.06229728227612, 1.8625668296819, 1.22004089065561,
0.472446924052171, 0.285314983167609, 0.210501378242261, 0.0694262373804251,
-0.0144675088601809, -0.926476372785716, -0.997731763217841,
-1.62164362525624, -2.12120181627754, -1.87179280480865, -1.64798056454827,
-1.5526995652836, -1.64049345720051), .borders = c(0.957710151765353,
1.13854777070064, 1.18522292993631, 1.22058598726115, 1.29930014940631,
1.33971974522293, 1.40121097743178, 1.48638829912715, 1.5, 1.52428025477707,
1.6371974522293, 1.65183612487222, 1.70405095541401, 1.80025477707006,
1.98361248722183, 2.07583549579303, 2.17044900526854, 2.22039789258473
), .feature = c("BD", "BD", "BD", "BD", "BD", "BD", "BD", "BD",
"BD", "BD", "BD", "BD", "BD", "BD", "BD", "BD", "BD", "BD")), row.names = c(NA,
-18L), class = "data.frame"), Sand = structure(list(.type = c("ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale"), .value = c(5.72243888922004, 5.04938965197184, 4.7017193997508,
4.38186986390524, 3.55049136618825, 3.79907192663079, 3.65192607277815,
2.69221076986002, 2.08417035244566, 0.508134498143654, -0.760157331236183,
-2.22196153094718, -2.60183427497522, -2.6090089814882, -4.29829842975557,
-4.89113681989785, -6.17719615317024, -6.51041335854504, -6.70721878970499,
-6.69985492661679), .borders = c(3.9999999999992, 7.99999999999983,
12.0000000000005, 20.0000000000003, 23.9999999999995, 27.9999999999987,
31.9999999999993, 39.9999999999991, 43.9999999999998, 48.0000000000004,
52.000000000001, 59.9999999999994, 63.9999999999986, 64.0000000000001,
68.0000000000007, 71.9999999999999, 76.0000000000005, 79.9999999999997,
84.0000000000003, 88.000000000001), .feature = c("Sand", "Sand",
"Sand", "Sand", "Sand", "Sand", "Sand", "Sand", "Sand", "Sand",
"Sand", "Sand", "Sand", "Sand", "Sand", "Sand", "Sand", "Sand",
"Sand", "Sand")), row.names = c(NA, -20L), class = "data.frame"),
Silt = structure(list(.type = c("ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale"), .value = c(-1.81467365524819,
-1.80595765781436, -1.44252832794551, -1.40678289959526,
-1.3809409627197, -1.27631650711468, -0.918535212874314,
-0.891479949841867, -0.130663280250859, 0.054226944600215,
0.0371390649359506, 0.547567627523539, 0.540716330465325,
1.14691203242341, 1.45243650519454, 1.51824963792443, 1.76292774269082,
1.85725359158924, 2.48581029669555, 0.388409658807059), .borders = c(0,
3.99999999999921, 7.99999999999841, 7.99999999999983, 8.00000000000125,
11.999999999999, 15.9999999999997, 16.0000000000011, 20.0000000000003,
23.9999999999995, 24.0000000000009, 27.9999999999987, 28.0000000000001,
31.9999999999993, 35.9999999999985, 40.0000000000006, 43.9999999999998,
47.999999999999, 64.0000000000015, 79.9999999999997), .feature = c("Silt",
"Silt", "Silt", "Silt", "Silt", "Silt", "Silt", "Silt", "Silt",
"Silt", "Silt", "Silt", "Silt", "Silt", "Silt", "Silt", "Silt",
"Silt", "Silt", "Silt")), row.names = c(NA, -20L), class = "data.frame"),
Clay = structure(list(.type = c("ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale", "ale",
"ale", "ale", "ale", "ale", "ale"), .value = c(-1.02199413478959,
-0.826304780697195, -0.942406575344331, -0.971009582383038,
-1.24981505440423, -1.24219672780758, -0.443954661148336,
-0.344646636267013, -0.344646636267013, 0.0190677799370838,
0.509523035852588, 0.805292224788913, 0.990611614682741,
0.998147256094054, 1.16179870265779, 2.06234942853985, 2.8900928986798,
3.82397972588229), .borders = c(0, 7.99999999999983, 11.999999999999,
12.0000000000005, 15.9999999999997, 16.0000000000011, 20.0000000000003,
23.9999999999995, 24.0000000000009, 28.0000000000001, 31.9999999999993,
35.9999999999999, 39.9999999999991, 40.0000000000006, 47.999999999999,
51.9999999999996, 56.0000000000002, 67.9999999999993), .feature = c("Clay",
"Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Clay",
"Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Clay", "Clay",
"Clay")), row.names = c(NA, -18L), class = "data.frame"))
2条答案
按热度按时间5cnsuln71#
UdpateOP请求见备注:
添加.value列:
**第一个答案:**要解决您得到的错误:
1.使用
purrr::transpose() %>%
$(.borders)
可以创建6个不同长度的向量列表。然后尝试用bind_rows将它们按列组合成一个tibble。错误是要实现你想要的功能,你可以使用
purrr::map_df()
或者它的变体purrr::imap_dfr()
:以下是使用
map_df
的版本:或
r1wp621o2#
使用
map()
和list_rbind()
:如果顺序不重要,您可以直接使用: