my_data:
LCE1B KIR2DL4 CDC20 CC2D1B STXBP3 LMLN SDAD1 NPAS4
NDUFC2 KIR3DL3 IKZF2 CCT6A TGFBR2 MMP24 PADI4 SCUBE2
SNRPD1 KRT33B INA CORO2A CDKL3 SRPK1 RNF145 SLC45A4
SSNA1 MBTD1 MATN1 FAAH2 SLC9A4 TBL3
TANK NOS1AP SPON1
TMEM14C
所需的输出:
NDUFC2 KRT33B CDC20 CORO2A TGFBR2 SRPK1 SDAD1 SCUBE2
TMEM14C KRT33B INA CC2D1B STXBP3 SPON1 PADI4 NPAS4
我尝试使用示例
sample(1:ncol(my_data), 2, replace=FALSE)
随机选择该行。我需要从每一栏中随机挑选。
答案 0 :(得分:0)
# create the MWE
my_data <- as.data.frame(matrix(sample(c("", letters[1:24]), 48, replace = TRUE), 6, 8))
apply(my_data, 2, function(col){
# get non empty values
cc <- col[col!=""]
cc[sample(seq(cc), 2, replace=FALSE)]
})