用R符号连接R中的两个单元格

时间:2016-02-10 13:15:49

标签: r

我想计算列表中成对镜像矩阵的均值和标准偏差,并为进一步的文本处理写一个表:

mean_SG<- as.data.frame(lapply(list_SG, function(x) mean(x[upper.tri(x)])))
sd_SG <- as.data.frame(lapply(list_SG, function(x) sd(x[upper.tri(x)])))
write.table(t(rbind(round(mean_SG,3),round(sd_SG,3))), "SG.txt")

我的想法是直接将mean_SG和sd_SG的数值与正负符号±连接起来,然后用write.table将它写在一个列中。这可能在R?

以下是一些数据:

SG <- structure(c(85, 84.016, 82.9, 79, 85.167, 83.467, 78.5, 83.051, 
80.064, 81.436, 79.94, 83.731, 83.468, 82.775, 83.294, 81.608, 
82.176, 84.138, 82.6, 85.325, 82.297, 81.546, 83.569, 84.561, 
87.039, 92.45, 86.35, 83.153, 84.447, 81.899, 81.972, 81.32, 
81.949, 82.101, 0.656, 0.966, 1.833, NA, 0.643, 0.459, 0.608, 
1.189, 1.024, 0.848, 1.207, 0.66, 0.757, 1.235, 0.872, 1.308, 
0.958, 1.151, 0.914, 1.302, 0.708, 0.79, 1.349, 0.799, 1.297, 
2.554, 0.55, 1.041, 1.216, 1.065, 0.981, 0.937, 1.133, 1.302), .Dim = c(34L, 
2L), .Dimnames = list(c("X19_vs_11B.2", "X19_vs_AT.s3.28", "X19_vs_B276.D12", 
"X19_vs_BP.U1C.1g10", "X19_vs_d142", "X19_vs_FFCH5909", "X19_vs_GBS.L1.B05", 
"X19_vs_SG01", "X19_vs_SG02", "X19_vs_SG03", "X19_vs_SG04", "X19_vs_SG05", 
"X19_vs_SG06", "X19_vs_SG07a", "X19_vs_SG07b", "X19_vs_SG08.Aca", 
"X19_vs_SG08.Holo", "X19_vs_SG09", "X19_vs_SG10", "X19_vs_SG11", 
"X19_vs_SG12", "X19_vs_SG13", "X19_vs_SG15", "X19_vs_SG17", "X19_vs_SG18", 
"X19_vs_SG19", "X19_vs_SG20", "X19_vs_SG21", "X19_vs_SG22", "X19_vs_SG23", 
"X19_vs_SG25", "X19_vs_SG26", "X19_vs_ThAna", "X19_vs_TPD.58"
), c("1", "2"))

1 个答案:

答案 0 :(得分:11)

我喜欢sprintf。它允许您指定位数。

sprintf("%.3f \U00B1 %.3f", SG[,1], SG[,2])
#[1] "85.000 ± 0.656" "84.016 ± 0.966" "82.900 ± 1.833" "79.000 ± NA"    "85.167 ± 0.643" "83.467 ± 0.459" ...