【问题标题】:how to randomly name columns or rows of a data frame?如何随机命名数据框的列或行?
【发布时间】:2015-03-10 10:29:10
【问题描述】:

有类似的问题可用,但没有一个像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 一样多的列或尽可能多的行?

【问题讨论】:

  • 您可以使用rep。 IE。 rep(paste( LETTERS, "col", sep =""), length.out=ncol(M)) 第一个问题。虽然你可以查看?sample,但我并没有完全理解第二部分
  • @akrun 谢谢akrun,这已经是一个很大的帮助,我会尝试根据样本找出第二个,如果我不能,我会发布一个更好的问题
  • 第二个问题的条件不清楚。你有一个Ccol,下一个是GFcol,等等。那么,col 之前的前缀字符数有什么限制吗?
  • @akrun 不,没有任何限制!只是唯一的列名

标签: r


【解决方案1】:

对于问题的第二部分(因为第一个问题似乎由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

【讨论】:

    【解决方案2】:

    akrun 给了你第一个答案: 代表(粘贴(字母,“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")
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2011-09-19
      • 2011-12-23
      • 1970-01-01
      • 2020-01-30
      • 2022-01-20
      • 1970-01-01
      • 2017-09-03
      • 2021-04-18
      相关资源
      最近更新 更多