我怎么能转换成一个列表到数据帧

时间:2019-02-01 12:23:01

标签: r list

我有16个项目的清单:

class ConditionalLockContext 
{
public:
    ConditionalLockContext(
        std::function<bool()> condition)
    : m_condition(condition)

private: 
    std::function<bool()> m_condition;
    bool m_shouldLock;
    bool m_lockInit;
}

我想拥有一个这样的数据帧,其中mu,szig和kszi是列。对于每一行,我想计算一个新值[[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 ... 并将其作为第四列添加到数据框中。

2 个答案:

答案 0 :(得分:0)

如果您希望列表中的元素成为数据框的列,则可以单独使用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

正如我在上面代码的注释中提到的那样,如果将来使用dput()命令的输出来发布示例数据,对于将来的答复者来说,它会更加有用。有关更多详细信息,请参见How to make a great R reproducible example

答案 1 :(得分:0)

类似于@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..."