Rename and combine rows in R

I am trying to combine a a couple of rows into a dataframe. So below is the code I am using to get the output dataframe.

myData <- reactive({
age_selected <- input$selected_age
gender_selected <- input$selected_gender
ethnicity_selected <- input$selected_ethnicity


df <- with_demo_vars %>%
  filter(age == age_selected) %>%
  filter(gender == gender_selected) %>%
  filter(ethnicity == ethnicity_selected) %>%
  pct_ever_user(type = "SM")

 #df[, c("variable",  "sum_wts", "se")]
 #interval=paste(df$ci_l,df$ci_u,collapse = "-")
 df <- mutate(df, intervals= paste("(",round(ci_l,digits = 3), round(ci_u,digits = 3),sep = "-",")"))
#   %>% c("variable", paste("mean",x), "sum_wts", "se")
 #df[c(("variable", "mean", "sum_wts", "se","x")]

})

The output what I get and what I want are as following:

Thank You.

Hi @snt

If i understand you correctly you could use the glue package which has some good samples here. It is part of the tidyverse library but can be called on its own too using library(glue). Based on my understanding of your needs the below should help you

library(tidyverse)
options(scipen = 999) # Turns off scientific notation

# Create a sample dataframe
mydf <- tibble(variable = "ever_user",
       level = NA,
       mean = 0.34,
       se = 0.01,
       ci_l = 0.31,
       ci_u = 0.37,
       n = 2418,
       sum_wts = 800000,
       intervals = "(-0315-0369-)"
)

# Create new columns and select a final output
mydf_calc <- mydf %>% 
  mutate(mean_calc = glue("{mean}{intervals}"),
         standard_error = glue("{se*100}%")) %>% 
  select(variable, mean_calc, standard_error, N = n, Weighted_N = sum_wts)

Hope this is useful

2 Likes