I would like to interlace two columns of a dataframe (move sd underneath the mean) similar to here, but my problem is that I have other columns in the same dataframe.
My code looks like this:
year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)
dta <- data.frame(year = year, group = group, value = value)
library(dplyr)
dta2 <- dta %>%
group_by(year, group) %>%
summarize(mean=mean(value), sd=sd(value)) %>%
# round:
dta2[,3:4] <- round(dta2[,3:4], 2)
# add parenthesis around sd:
dta2$sd <- paste0("(", dta2$sd, ")")
# Now I would like to interlace mean and sd:
dta3 <- data.frame(c(rbind(dta2[1:3], dta2[3])))
This gives me an error, because of the other columns in the dataframe.
# This is the output I would like: