labels with numbers under a column in dataframe

I have the following code to create a dataframe:

df <- data.frame(

   label = c(lower = model$"0.025quant"[[1]],

             mean = model$mean[[1]],
             upper = model$"0.975quant"[[1]]),

   cyl   = c("East", "East", "East"),

   x     = c(4.5, 4.5, 4.5),

   y     = c(0.075, 4, 4.5)


The problem is only in the “label” part of df. The values of model$"0.025quant"[1] which are extracted and put into the “label” column are decimals, like 0.0005334041. First, I want them to be rounded to three decimal places in df, like 0.001. Second, I want the values under “label” column to look like the following “lower=0.001”, “mean=0.005”, “upper=0.020”. Something like that:


But only with the rest of the columns “cyl”, “x” and “y”.

In other words, first, the values need to be extracted from the list “model”, then put in a rounded form into df, and finally look like the labels ““lower=0.001”, etc.

Guys, do you think this is something feasible to do in R? Could you suggest how my code should be modified so as to produce the desired dataframe?

Is this close enough?

model <- list("0.025quant" = 0.00053, mean = 0.0054, "0.975uant" = 0.02035)

ModelValues <- formatC(round(c(model$`0.025quant`[[1]], 
                       model$`0.975uant`[[1]]), 3),digits = 3)
df <- data.frame(
  label = paste(c("lower","mean","upper"), "=", ModelValues),
  cyl   = c("East", "East", "East"),
  x     = c(4.5, 4.5, 4.5),
  y     = c(0.075, 4, 4.5)
#>           label  cyl   x     y
#> 1 lower = 0.001 East 4.5 0.075
#> 2  mean = 0.005 East 4.5 4.000
#> 3  upper = 0.02 East 4.5 4.500

Created on 2022-06-10 by the reprex package (v2.0.1)

This is genious enough) Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.