我有以下数据:
data=structure(list(kod_nar.id = c(1L, 3L, 2L), x123 = c(0L, 0L, 0L
), x124 = c(0L, 0L, 0L), x125 = c(0L, 0L, 0L), x126 = c(0L, 0L,
0L)), .Names = c("kod_nar.id", "x123", "x124", "x125", "x126"
), class = "data.frame", row.names = c(NA, -3L))
我需要为每列_1
,_2
等添加后缀。
输出可能是
kod_nar.id x123_1 x124_2 x125_3 x126_4
1 1 0 0 0 0
2 3 0 0 0 0
3 2 0 0 0 0
后缀按升序排列。
然后对行相同
kod_nar.id x123_1 x124_2 x125_3 x126_4
1 1_1 0 0 0 0
2 3_2 0 0 0 0
3 2_3 0 0 0 0
我该怎么做?
答案 0 :(得分:4)
类似的东西:
O(n^2)
答案 1 :(得分:2)
您甚至可以尝试--
> names(data)[-1] <- paste0(names(data)[-1],"_",1:(ncol(data)-1))
> data
kod_nar.id x123_1 x124_2 x125_3 x126_4
1 1 0 0 0 0
2 3 0 0 0 0
3 2 0 0 0 0
> data.table::setDT(data)[,kod_nar.id:=paste0(kod_nar.id,"_",1:nrow(data))]
> data
kod_nar.id x123_1 x124_2 x125_3 x126_4
1: 1_1 0 0 0 0
2: 3_2 0 0 0 0
3: 2_3 0 0 0 0