Я пытаюсь обобщить этот кусок кода:
trimmedMeans %>%
mutate(Expectation_mean = paste(format(Expectation_mean, digits = 2, nsmall = 2),
"±",
format(Expectation_sd, digits = 2, nsmall = 2)),
Interesting_mean = paste(format(Interesting_mean, digits = 2, nsmall = 2),
"±",
format(Interesting_sd, digits = 2, nsmall = 2)),
Useful_mean = paste(format(Useful_mean, digits = 2, nsmall = 2),
"±",
format(Useful_sd, digits = 2, nsmall = 2)),
OralPresentation_mean = paste(format(OralPresentation_mean, digits = 2, nsmall = 2),
"±",
format(OralPresentation_sd, digits = 2, nsmall = 2))
)
Я пытаюсь сделать это:
paste.Mean.Sd <- function(m, s){
paste(format(m, digits = 2, nsmall = 2),
"±",
format(s, digits = 2, nsmall = 2)) }
trimmedMeans2 <- trimmedMeans %>%
mutate_at(vars(contains('_mean')), funs(paste.Mean.Sd(
vars(contains('_mean')), vars(contains('_sd'))
)) )
То, что я получаю, выглядит примерно так:
То, что я ожидал получить, это:
Чего мне не хватает?
РЕДАКТИРОВАТЬ 1
Этот код дает мне правильный результат для "левой части" (среднего) строки, а не длячасть SD:
trimmedMeans %>%
mutate_at(vars(contains('_mean')), funs(paste.Mean.Sd(., str_replace(., "_mean", "_sd"))))
РЕДАКТИРОВАТЬ 2
Ниже приведен коддля воспроизведения кадра данных я использовал:
trimmedMeans <- structure(list(TrackName = structure(c(2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Llytse", "Mneshe", "Phrypa", "Veormi"), class = "factor"),
SpeakerName = c("Delta Shelby", "Irvine Fairburn", "Kristine Harland",
"Paislee Jež", "Rhianna Clarke", "Spencer Hargrave"), NumOfVoters = c(15L,
14L, 5L, 14L, 17L, 19L), Expectation_mean = c(4.6, 5, 4.2,
4.07142857142857, 4.41176470588235, 4.73684210526316), Interesting_mean = c(4.46666666666667,
5.5, 5, 4.78571428571429, 5.05882352941176, 5.57894736842105
), Useful_mean = c(4.6, 5.14285714285714, 4.6, 4.28571428571429,
4.52941176470588, 5.42105263157895), OralPresentation_mean = c(4.33333333333333,
5.28571428571429, 5.4, 4.85714285714286, 5.17647058823529,
5.52631578947368), Expectation_sd = c(0.736788397613007,
0.784464540552736, 0.836660026534076, 0.474631146549323,
0.870260272089029, 0.561951486949016), Interesting_sd = c(0.639940473422184,
0.518874521662771, 0.707106781186548, 0.801783725737273,
0.747545001596402, 0.507257273501788), Useful_sd = c(0.9102589898328,
1.02710518202619, 0.894427190999916, 0.913873533463375, 1.06757008311068,
0.507257273501788), OralPresentation_sd = c(0.975900072948533,
0.825420305855557, 0.547722557505166, 0.864437821507567,
0.63593377383646, 0.611775290321498)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), vars = c("TrackName",
"SpeakerName"), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 4L,
5L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
TrackName = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Llytse",
"Mneshe", "Phrypa", "Veormi"), class = "factor"), SpeakerName = c("Delta Shelby",
"Irvine Fairburn", "Kristine Harland", "Paislee Jež", "Rhianna Clarke",
"Spencer Hargrave")), class = "data.frame", row.names = c(NA,
-6L), vars = c("TrackName", "SpeakerName"), drop = TRUE, .Names = c("TrackName",
"SpeakerName")), .Names = c("TrackName", "SpeakerName", "NumOfVoters",
"Expectation_mean", "Interesting_mean", "Useful_mean", "OralPresentation_mean",
"Expectation_sd", "Interesting_sd", "Useful_sd", "OralPresentation_sd"
))