【问题标题】:Create Vector of Factors from random labelling of rows of a data frame从数据框行的随机标记创建因子向量
【发布时间】:2017-07-26 08:46:38
【问题描述】:

我有一个包含 110 行的数据框,它是来自微阵列实验表达式集对象的 pData。我想创建一个具有 2 个级别的因子向量,随机分配给行(代表实验的样本)。例如,如果有 110 行对应于实验中的 110 名受试者,我希望将 55 行设置为“G0”,将 55 行设置为“G1”。这些组用于后续功能。 我目前正在尝试以下内容,它包含在我要修改的函数中:

# makes a numeric vector of the number of subjects/rows in the pData
sml<-rep(0,length(colnames(eset))

# ‘populate’ sml with G0 & G1 
sml[sample(sml,(length(sml)/2))]<-"G0"
sml[sample(sml,(length(sml)/2))]<-"G1"
label <- as.factor(sml)

如何进行采样,以使 G1 组完成 sml 的长度,并使已分配为 G0 的位置保持不变? 谢谢

【问题讨论】:

    标签: r random bioconductor


    【解决方案1】:

    这是正确答案

    eset <- matrix(NA, ncol = 110, nrow = 1)
    good <- sample(
      rep(
        factor(c("G0", "G1")),
        ncol(eset) %/% 2
      )
    )
    table(good)
    

    这是一个不好的例子

    bad <- sample(c("G0", "G1"), ncol(eset), replace = TRUE)
    table(bad)
    

    【讨论】:

    • 太棒了,这个我不知道!
    猜你喜欢
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多