我正在尝试使用col和row名称创建一个空矩阵:
tbl = list.files(pattern="*.csv")
col_names <- unique(tbl)
mat_master_EOD <- matrix(0, nrow = length(data_rd[,1]),ncol = 82)
colnames(mat_master_EOD) <- col_names
rownames(mat_master_EOD) <- data_rd[,1]
这就是列的名称应该是这样的。
> tbl
[1] "F1.csv" "F10.csv" "F11.csv" "F12.csv" "F13.csv" "F14.csv" "F15.csv" "F16.csv" "F17.csv" "F18.csv"
[11] "F19.csv" "F2.csv" "F20.csv" "F3.csv" "F4.csv" "F5.csv" "F6.csv" "F7.csv" "F8.csv" "F9.csv"
问题是我有80列,只有20个名字。我想为每个名称设置4列,首先应该是“Accession”和第二个“Description”。应该是这样的:
Accession Description F1.csv_1 F1.csv_2 F1.csv_3 F1.csv_4 F2.csv_1 F2.csv_2 F2.csv_3 and so on...
答案 0 :(得分:2)
每个'col_names'创建四个名称的一种方法:
col_names <- c("F1.csv", "F10.csv")
paste0(rep(col_names, each = 4), "_", 1:4)
# [1] "F1.csv_1" "F1.csv_2" "F1.csv_3" "F1.csv_4" "F10.csv_1" "F10.csv_2" "F10.csv_3"
# [8] "F10.csv_4"