简体   繁体   English

在 R 中,将 data.frame 转换为不包含列表的标准 tibble

[英]In R tranform data.frame to a standard tibble not comprising lists

How can I transform the below data.frame to a comparable/traditional tibble (ie, not containing lists);如何将以下 data.frame 转换为可比较/传统的小标题(即不包含列表); and why is this behavior occurring/can it be avoided?为什么会发生这种行为/可以避免吗?

df_test <- structure(list(Dim1 = structure(list(0.1, 4, NA_real_, NA_real_, 
                                     NA_real_), .Names = c("one", "two", "adfae", NA, "")), Dim2 = structure(list(
                                       2, 5, NA_real_, NA_real_, NA_real_), .Names = c("one", "two", 
                                                                                       "adfae", NA, "")), Dim3 = structure(list(3, 6, NA_real_, NA_real_, 
                                                                                                                                NA_real_), .Names = c("one", "two", "adfae", NA, ""))), class = "data.frame", row.names = c("one", 
                                                                                                                                                                                                                            "two", "adfae", "NA.", "X"))
tbble_test <- as_tibble(df_test)
tibble_test

Thanks in advance提前致谢

Each of the column is a list ,每一列都是一个list

str(df_test)
#'data.frame':  5 obs. of  3 variables:
# $ Dim1:List of 5
#  ..$ one  : num 0.1
#  ..$ two  : num 4
#  ..$ adfae: num NA
#  ..$ NA   : num NA
#  ..$      : num NA
# $ Dim2:List of 5
#  ..$ one  : num 2
#  ..$ two  : num 5
#  ..$ adfae: num NA
#  ..$ NA   : num NA
#  ..$      : num NA
# $ Dim3:List of 5
#  ..$ one  : num 3
#  ..$ two  : num 6
#  ..$ adfae: num NA
#  ..$ NA   : num NA
#  ..$      : num NA

we can first unlist and it should work我们可以先unlist ,它应该可以工作

df_test[] <- lapply(df_test, unlist)
as_tibble(df_test)

Or using tidyverse或使用tidyverse

library(dplyr)
df_test %>%
     mutate(across(everything(), unlist)) %>% 
     as_tibble

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM