gpt4 book ai didi

r - 如何将列表值列拆分为多列?

转载 作者:行者123 更新时间:2023-12-04 09:28:17 25 4
gpt4 key购买 nike

我有以下情况,专栏power_dbm0具有列表形式的值。所有元素都是长度为 11 的列表。

# A tibble: 10 x 2
real_pat power_dbm0
<chr> <list>
1 am <dbl [11]>
2 fax <dbl [11]>
3 fp <dbl [11]>
4 fpw <dbl [11]>

我想知道如何拆分这些值,因为每个订单都是一个新列。最好,我想要一个类似 dplyr 的解决方案。我已经尝试了一些解决方案 unnestseparate函数来自 tidyr ,但没有成功。

提前致谢,

关注数据:
structure(list(real_pat = c("am", "fax", "fp", "fpw"), power_dbm0 = list(
structure(c(0.0142857142857143, 0.0742857142857143, 0.111428571428571,
0.138571428571429, 0.208571428571429, 0.278571428571429,
0.368571428571429, 0.508571428571429, 0.648571428571429,
0.771428571428571, 0.871428571428571), .Names = c("0%", "10%",
"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"
)), structure(c(0.342857142857143, 0.342857142857143, 0.342857142857143,
0.342857142857143, 0.342857142857143, 0.342857142857143,
0.342857142857143, 0.342857142857143, 0.342857142857143,
0.342857142857143, 0.342857142857143), .Names = c("0%", "10%",
"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"
)), structure(c(0.0142857142857143, 0.622857142857143, 0.808571428571429,
0.851428571428571, 0.857142857142857, 0.871428571428571,
0.874285714285714, 0.885714285714286, 0.894285714285714,
0.911428571428571, 0.914285714285714), .Names = c("0%", "10%",
"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"
)), structure(c(0.514285714285714, 0.514285714285714, 0.514285714285714,
0.514285714285714, 0.514285714285714, 0.514285714285714,
0.514285714285714, 0.514285714285714, 0.514285714285714,
0.514285714285714, 0.514285714285714), .Names = c("0%", "10%",
"20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"
)))), .Names = c("real_pat", "power_dbm0"), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))

最佳答案

1) 这是一个单行基本解决方案:

with(dd, do.call("rbind", setNames(power_dbm0, real_pat)))

给予:
            0%        10%       20%       30%       40%       50%       60%
am 0.01428571 0.07428571 0.1114286 0.1385714 0.2085714 0.2785714 0.3685714
fax 0.34285714 0.34285714 0.3428571 0.3428571 0.3428571 0.3428571 0.3428571
fp 0.01428571 0.62285714 0.8085714 0.8514286 0.8571429 0.8714286 0.8742857
fpw 0.51428571 0.51428571 0.5142857 0.5142857 0.5142857 0.5142857 0.5142857
70% 80% 90% 100%
am 0.5085714 0.6485714 0.7714286 0.8714286
fax 0.3428571 0.3428571 0.3428571 0.3428571
fp 0.8857143 0.8942857 0.9114286 0.9142857
fpw 0.5142857 0.5142857 0.5142857 0.5142857

2) 或获取 real_pat作为列而不是名称:
with(dd, data.frame(real_pat, do.call("rbind", power_dbm0), check.names = FALSE))

给予:
  real_pat         0%        10%       20%       30%       40%       50%
1 am 0.01428571 0.07428571 0.1114286 0.1385714 0.2085714 0.2785714
2 fax 0.34285714 0.34285714 0.3428571 0.3428571 0.3428571 0.3428571
3 fp 0.01428571 0.62285714 0.8085714 0.8514286 0.8571429 0.8714286
4 fpw 0.51428571 0.51428571 0.5142857 0.5142857 0.5142857 0.5142857
60% 70% 80% 90% 100%
1 0.3685714 0.5085714 0.6485714 0.7714286 0.8714286
2 0.3428571 0.3428571 0.3428571 0.3428571 0.3428571
3 0.8742857 0.8857143 0.8942857 0.9114286 0.9142857
4 0.5142857 0.5142857 0.5142857 0.5142857 0.5142857

3) 使用 dplyr 我们可以这样写:
library(dplyr)
dd %>% { bind_cols(select(., real_pat), bind_rows(!!!.$power_dbm0)) }

给予:
# A tibble: 4 x 12
real_pat `0%` `10%` `20%` `30%` `40%` `50%` `60%` `70%` `80%` `90%` `100%`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 am 0.0143 0.0743 0.111 0.139 0.209 0.279 0.369 0.509 0.649 0.771 0.871
2 fax 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343
3 fp 0.0143 0.623 0.809 0.851 0.857 0.871 0.874 0.886 0.894 0.911 0.914
4 fpw 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514

3a) 或使用 .id= bind_rows 的论据和 magrittr %$% :
library(dplyr)
library(magrittr)

dd %$%
setNames(power_dbm0, real_pat) %$%
bind_rows(!!!., .id = "real_pat")

给予:
# A tibble: 4 x 12
real_pat `0%` `10%` `20%` `30%` `40%` `50%` `60%` `70%` `80%` `90%` `100%`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 am 0.0143 0.0743 0.111 0.139 0.209 0.279 0.369 0.509 0.649 0.771 0.871
2 fax 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343
3 fp 0.0143 0.623 0.809 0.851 0.857 0.871 0.874 0.886 0.894 0.911 0.914
4 fpw 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514

3b) 或没有 %$% :
library(dplyr)

dd %>%
{ setNames(.$power_dbm0, .$real_pat) } %>%
{ bind_rows(!!!., .id = "real_pat") }

给予:
# A tibble: 4 x 12
real_pat `0%` `10%` `20%` `30%` `40%` `50%` `60%` `70%` `80%` `90%` `100%`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 am 0.0143 0.0743 0.111 0.139 0.209 0.279 0.369 0.509 0.649 0.771 0.871
2 fax 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343 0.343
3 fp 0.0143 0.623 0.809 0.851 0.857 0.871 0.874 0.886 0.894 0.911 0.914
4 fpw 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514 0.514

关于r - 如何将列表值列拆分为多列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com