有类似的问题可用,但没有一个像Changing column names of a data frame in R那样解决这个问题
实际上,我有一个矩阵,如下所示
M <- data.frame(matrix(rnorm(5),100,50))我试着为它的每一列列一个名字列表,如下所示:
colnames(M) <- paste( LETTERS, "col", sep ="")如果列数等于或小于字母数,这将起作用。如果我想
1-在丰富结尾后重复字母
2-随机地为每一列生成名称,其中包含特定的单词,但是随机的字母,如Ccol GFcol Mercol,它有多少列或多少行?
发布于 2015-03-10 19:21:09
对于问题的第二部分(第一个问题似乎已经由akrun解决),您可以尝试:
# Generate unique combinations of at most three letters
LET <- apply(expand.grid(LETTERS, LETTERS, LETTERS)[sample(1:676, dim(M)[2]),], 1, function(x) x[sample(1:3, sample(1:3))])
colnames(M) <- paste0(sapply(LET, paste0, collapse = ""), "col")这就给出了:
head(M, 2)
AZFcol OJcol Gcol ALPcol NAcol VAcol KEcol Acol VBcol HAcol
1 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018
2 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753
KYcol AARcol Wcol EAcol OTAcol AMcol AAcol QAcol Acol AMcol
1 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018
2 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753
AScol DQcol Bcol Jcol BAcol AIcol WEcol SAUcol Acol Acol
1 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018
2 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753
RAOcol JAcol GAEcol ABQcol BAcol TAcol AAMcol ACEcol Kcol NAcol
1 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018
2 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753
Bcol HAEcol ABcol AVDcol Hcol AQcol WHcol KIAcol QLcol FRcol
1 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018 -1.842018
2 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753 1.069753https://stackoverflow.com/questions/28961297
复制相似问题