mercredi 26 juillet 2017

Create Vector of Factors from random labelling of rows of a data frame

I have a dataframe with 110 rows which is the pData from a microarray experiment expressionset object. I want to create a vector of factors with 2 levels, randomly assigned to the rows (which represent the samples of the experiment). For example, if there are 110 rows corresponding to 110 subjects in the experiment I would want 55 rows to be set as “G0” and 55 as “G1”. These groups are used in a subsequent function. I am currently trying the following which is wrapped within a function I am trying to modify:

# 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)

How do I sample such that the G1 group completes the length of sml and leaves the positions already assigned as G0 untouched? Thanks




Aucun commentaire:

Enregistrer un commentaire