简体   繁体   中英

how can I convert a list to a dataframe

I have this list of 16 items:

[[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
...

I would like to have such a dataframe where mu, szig, and kszi are columns. For each row I want to calculate a new value 1-exp(-(1+kszi*((0-mu)/szig))^(-1/kszi)) and add that to the dataframe as a 4th column.

If you intended the elements of your list to be the columns of your dataframe, you could use as.data.frame(l) by itself (where l is whatever the name of your list is). Since you want the elements of your list to be the rows of your dataframe, we also have to enlist the help of 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

As I mention in the comments to the code above, for future reference it is much more helpful to potential answerers if you post your example data using the output of the dput() command. See How to make a great R reproducible example for more details.

Similar to @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..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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