我的资料
conc_data=structure(list(kod_nar.id = c(1L, 3L, 2L),
x123_1 = c(0L, 0L, 0L),
x124_2 = c(0, 0.123, 0.122),
x125_3 = 0:2,
x126_4 = c(0, 0.234, 0.99)),
.Names = c("kod_nar.id", "x123_1", "x124_2", "x125_3", "x126_4"),
class = "data.frame", row.names = c(NA, -3L))
Run Code Online (Sandbox Code Playgroud)
这里有4列,但是每2列需要合并为一列,并具有第一列的名称。换句话说,每对列都应合并在一起,并串联数字的值。结果,我们在数据框中将只有2列。数据帧中的每一列都有一对,列数是偶数,列的顺序是第一对,第二对,依此类推
IE输出
kod_nar.id x123_1 x125_3
1 1 0 0
2 3 0(0.123) 1(0.234)
3 2 0(0.122) 2(0.99)
Run Code Online (Sandbox Code Playgroud)
怎么做?
要么:
conc_data$x123_1 <- with(conc_data, ifelse(x124_2 == 0, "0", sprintf("%d(%.3f)", x123_1, x124_2)))
conc_data$x125_3 <- with(conc_data, ifelse(x126_4 == 0, "0", sprintf("%d(%.3f)", x125_3, x126_4)))
Run Code Online (Sandbox Code Playgroud)