有类似的问题可用,但没有一个像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.069753发布于 2015-03-10 19:31:38
akrun给出了第一个问题的答案: rep(paste( LETTERS,"col",sep =""),length.out=ncol(M))
对于第二种情况,我看到的唯一困难是避免对相同的字母进行重新采样,以获得唯一的列号。这就像在基数26中计数一样,所以你可以首先在这个基数中计数,直到你的列数:
GetNumberSuiteAnyBase <- function(lengthSuite,base){
nB <- length(base) # radix of your base
nDigits <- floor(log(lengthSuite-1)/log(nB))+1 # the number of digits you'll need
numberSuite <- ""
for(iDigit in 1:nDigits){
newDigit <- rep(base,each=nB^(iDigit-1),length.out=lengthSuite)
numberSuite <- paste0(newDigit,numberSuite)
}
return(numberSuite)
}
library("testthat")
# as an example:
expect_equal(as.numeric(GetNumberSuiteAnyBase(5,c(0,1))),c(0,1,10,11,100))
# with your requirements
colNames <- GetNumberSuiteAnyBase(ncol(M),LETTERS)然后,如果您希望这些列名是随机的,您可以只使用:
colNames <- paste0(sample(colNames),"col")https://stackoverflow.com/questions/28961297
复制相似问题