In R, how can I merge two data.frame columns using parentheses () as separation? -
how can merge 2 columns, 1) having second 1 between parentheses or 2) having +- symbol separation?
a <- data.frame(round(replicate(5, runif(10)),2)) a$collated <- paste(a$x1, a$x2, sep = " ")
this output want have:
x1 x2 x3 x4 x5 collated 1 0.46 0.42 0.47 0.41 0.58 0.46 ± 0.42 2 0.59 0.61 0.26 0.78 0.09 0.59 ± 0.61 3 0.17 0.48 0.18 0.61 0.49 0.17 ± 0.48
or one:
x1 x2 x3 x4 x5 collated 1 0.46 0.42 0.47 0.41 0.58 0.46 (0.42) 2 0.59 0.61 0.26 0.78 0.09 0.59 (0.61) 3 0.17 0.48 0.18 0.61 0.49 0.17 (0.48)
eventually, using kable() or similar function produce table.
we can use sprintf
a$collated <- sprintf("%2.2f ± %2.2f", a$x1, a$x2) # x1 x2 x3 x4 x5 collated #1 0.46 0.42 0.47 0.41 0.58 0.46 ± 0.42 #2 0.59 0.61 0.26 0.78 0.09 0.59 ± 0.61 #3 0.17 0.48 0.18 0.61 0.49 0.17 ± 0.48
or
a$collated <- sprintf("%2.2f (%2.2f)", a$x1, a$x2) # x1 x2 x3 x4 x5 collated #1 0.46 0.42 0.47 0.41 0.58 0.46 (0.42) #2 0.59 0.61 0.26 0.78 0.09 0.59 (0.61) #3 0.17 0.48 0.18 0.61 0.49 0.17 (0.48)
data
a <- structure(list(x1 = c(0.46, 0.59, 0.17), x2 = c(0.42, 0.61, 0.48 ), x3 = c(0.47, 0.26, 0.18), x4 = c(0.41, 0.78, 0.61), x5 = c(0.58, 0.09, 0.49)), .names = c("x1", "x2", "x3", "x4", "x5"), class = "data.frame", row.names = c("1", "2", "3"))
Comments
Post a Comment