【问题标题】:how can I convert a list to a dataframe如何将列表转换为数据框
【发布时间】:2019-06-26 00:40:39
【问题描述】:

我有这个 16 项的清单:

[[1]]
[1] -3.3354997  0.2301914  1.0979842

[[2]]
[1] -3.3275922  0.2505644  0.8881143

[[3]]
[1] -3.3743078  0.3318792  0.4635529

[[4]]
[1] -3.4310944  0.3303742  0.4707966

[[5]]
[1] -3.5093978  0.3527943  0.3970423
...

我想要这样一个数据框,其中 mu、szig 和 kszi 是列。对于每一行,我想计算一个新值 1-exp(-(1+kszi*((0-mu)/szig))^(-1/kszi)) 并将其作为第 4 列添加到数据框中。

【问题讨论】:

  • 您可能想查看help("as.data.frame")

标签: r list


【解决方案1】:

如果您希望列表的元素成为数据框的列,则可以单独使用as.data.frame(l)(其中l 是您列表的名称)。由于您希望列表的元素成为数据框的行,因此我们还必须寻求 do.call("rbind", l) 的帮助:

# Since you decided not to helpfully provide your data in an easily usable
# form, such as via the output of dput(), I recreate some of it here:
l <- list(c(-3.3354997,  0.2301914,  1.0979842),
          c(-3.3275922,  0.2505644,  0.8881143))
# We can use as.data.frame() on the output of do.call("rbind", l) to make
# the dataframe you're looking for:
(l_df <- as.data.frame(do.call("rbind", l)))
#          V1        V2        V3
# 1 -3.335500 0.2301914 1.0979842
# 2 -3.327592 0.2505644 0.8881143
# Here's the column name you wanted:
names(l_df) <- c("mu", "szig", "kszi")
# We can then add your new column normally:
l_df$new_column <- 1-exp(-(1+l_df$kszi*((0-l_df$mu)/l_df$szig))^(-1/l_df$kszi))
# And let's take a look at the result:
l_df
#          mu      szig      kszi new_column
# 1 -3.335500 0.2301914 1.0979842 0.07328838
# 2 -3.327592 0.2505644 0.8881143 0.05511381

正如我在上面代码的 cmets 中提到的,为了将来参考,如果您使用 dput() 命令的输出发布示例数据,这对潜在的回答者会更有帮助。详情请见How to make a great R reproducible example

【讨论】:

    【解决方案2】:

    类似于@duckmayr:

    # libraries ------------------------------------------------------------------------
    
    library(dplyr)                    # you don't really need it but %>% looks great
    library(purrr)                    # i use it to create example dataset only
    
    # example data ---------------------------------------------------------------------
    
    l <- replicate(16,                                        
                   map_dbl(c(-3, 0, 1), ~ rnorm(1, mean = .x)), 
                   simplify = F
                   )
    
    # explore the data -----------------------------------------------------------------
    
    glimpse(l)                         
    
    List of 16
     $ : num [1:3] -1.589 0.434 2.359
     $ : num [1:3] -3.962 0.347 2.985
     $ : num [1:3] -3.765 -0.115 0.117
     .................................
     $ : num [1:3] -2.125 -0.116 1.363
    
    # convert list 2 data.frame, do some math ------------------------------------------
    
    dat <- setNames(                      # set names for columns of returned data.frame
      do.call(rbind.data.frame, l),       # use rbind.data.frame to return data.frame
      c('mu', 'szig', 'kszi')             # vector of column names
      ) %>%                               # pipe resulting data.frame into mutate do math
      mutate(newcol = 1 - exp(-(1 + kszi * (-mu / szig))^(-1 / kszi)))
    
    # examine the results --------------------------------------------------------------
    
    head(dat)
    
             mu       szig      kszi    newcol
    1 -1.589332  0.4341922 2.3591750 0.3180414
    2 -3.962315  0.3472064 2.9853146 0.2619436
    3 -3.764680 -0.1152990 0.1165112       NaN
    4 -3.068161  0.9600714 1.6944156 0.2838741
    5 -2.305584 -0.5308407 2.9096659       NaN
    6 -2.671408  3.3653275 0.9817123 0.4265145
    
    # post your answer to stackoverflow -------------------------------------------------
    
    [1] "in progress..."
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2020-07-29
      • 2019-09-13
      • 2020-03-04
      • 1970-01-01
      • 2020-04-15
      • 2020-04-17
      相关资源
      最近更新 更多