gpt4 book ai didi

r - 如何将变量名绑定(bind)到df

转载 作者:行者123 更新时间:2023-12-04 07:23:31 24 4
gpt4 key购买 nike

我有一个如下列表:

lst<-list(Demographics = list(`101-01-101` = structure(list(SubjectID = "101-01-101", 
BRTHDTC = "1953-07-07", SEX = "Female"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `101-02-102` = structure(list(
SubjectID = "101-02-102", BRTHDTC = "1963-07-02", SEX = "Female"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `101-03-103` = structure(list(
SubjectID = "101-03-103", BRTHDTC = "1940-09-11", SEX = "Male"), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))), DiseaseStatus = list(
`101-01-101` = structure(list(SubjectID = "101-01-101", DSDT = "2016-03-14",
DSDT_P = NA_character_), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame")), `101-02-102` = structure(list(SubjectID = "101-02-102",
DSDT = "2017-04-04", DSDT_P = NA_character_), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame")), `101-03-103` = structure(list(
SubjectID = character(0), DSDT = character(0), DSDT_P = character(0)), row.names = integer(0), class = c("tbl_df",
"tbl", "data.frame"))), Visits = list(`101-01-101` = structure(list(
SubjectID = "101-01-101", Visit = "Screening: -28 Days to Day 1",
VISND = NA_character_), row.names = c(NA, -1L), class = c("tbl_df",
"tbl", "data.frame")), `101-02-102` = structure(list(SubjectID = character(0),
Visit = character(0), VISND = character(0)), row.names = integer(0), class = c("tbl_df",
"tbl", "data.frame")), `101-03-103` = structure(list(SubjectID = "101-03-103",
Visit = "Screening: -28 Days to Day 1", VISND = NA_character_), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"))))
我想将变量的名称绑定(bind)到每个 df 作为第一行,我该怎么做?我的代码如下:
lst1<-map(lst, function(x) map(x, ~rbind(names(.x), .x)))
但是,当df没有输入时,它会像这样将.x添加到变量名中:
enter image description here
enter image description here
我应该如何纠正这个?
我希望它是:
enter image description here
附加问题:
如果我们使用 rbind在下面,为什么它认为这个df没有名字?
enter image description here
对我来说,看起来我们有名称为 SubjectID、Visit、VISND 的变量。

最佳答案

我们可能需要

library(dplyr)
library(purrr)
map(lst, function(x) map(x,
~ bind_rows(setNames(as.list(names(.x)), names(.x)), .x)))
-输出
$Demographics
$Demographics$`101-01-101`
# A tibble: 2 x 3
SubjectID BRTHDTC SEX
<chr> <chr> <chr>
1 SubjectID BRTHDTC SEX
2 101-01-101 1953-07-07 Female

$Demographics$`101-02-102`
# A tibble: 2 x 3
SubjectID BRTHDTC SEX
<chr> <chr> <chr>
1 SubjectID BRTHDTC SEX
2 101-02-102 1963-07-02 Female

$Demographics$`101-03-103`
# A tibble: 2 x 3
SubjectID BRTHDTC SEX
<chr> <chr> <chr>
1 SubjectID BRTHDTC SEX
2 101-03-103 1940-09-11 Male


$DiseaseStatus
$DiseaseStatus$`101-01-101`
# A tibble: 2 x 3
SubjectID DSDT DSDT_P
<chr> <chr> <chr>
1 SubjectID DSDT DSDT_P
2 101-01-101 2016-03-14 <NA>

$DiseaseStatus$`101-02-102`
# A tibble: 2 x 3
SubjectID DSDT DSDT_P
<chr> <chr> <chr>
1 SubjectID DSDT DSDT_P
2 101-02-102 2017-04-04 <NA>

$DiseaseStatus$`101-03-103`
# A tibble: 1 x 3
SubjectID DSDT DSDT_P
<chr> <chr> <chr>
1 SubjectID DSDT DSDT_P

关于r - 如何将变量名绑定(bind)到df,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68352281/

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