Reputation: 1423
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 I want is a formatted and just a few fields:
Thank You.
Upvotes: 1
Views: 326
Reputation: 18681
You can create Mean
and Standard
within mutate
, then select the wanted columns while renaming n
and sum_wts
:
library(dplyr)
df <- mutate(df, intervals= paste("(",round(ci_l,digits = 3),
round(ci_u,digits = 3),
sep = "-",")"),
Mean = paste(mean, intervals),
Standard = paste0(se*100, "%")) %>%
select(variable, Mean, Standard, N = n, `Weighted N` = sum_wts)
Upvotes: 1
Reputation: 4243
You can first create a new column with the paste function to combine the two columns mean
and intervals
. Then select the columns in the order you want. And then rename each column.
df$Mean1<-paste(df$mean,df$intervals,sep=" ")
new_df<-select(df, variable, Mean1, se, n, sum_wts)
colnames(new_df)[1]<-"variable"
colnames(new_df)[2]<-"Mean"
colnames(new_df)[3]<-"Standard"
colnames(new_df)[4]<-"N"
colnames(new_df)[5]<-"Wighted N"
Upvotes: 1