【问题标题】:Randomly selecting columns then displaying the rest of the non randomly selected data [duplicate]随机选择列然后显示其余非随机选择的数据[重复]
【发布时间】:2019-01-27 22:31:18
【问题描述】:

代码如下:

df 是我的数据集 128 x 5000

test_random  <- df[, sample(ncol(df), 32)] 
training_random <-  df[-test_random, ] 

错误信息:

df[test_random, ] 中的错误:
只有 0 可以与负下标混合

关于如何显示 training_random 子集的任何想法?

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    你几乎完成了:

    test_random  <- df[, -sample(ncol(df), 32)] 
    
    
    ncol(test_random)
    

    【讨论】:

    • 如果 OP 同时需要 32 列 df 和另一个 df,请致电 sample 一次:inx &lt;- sample(etc)。然后df[, inx]df[, -inx] 是OP 想要的两个df。
    • @Salman 是的,谢谢你的工作。非常感谢
    • @AllenRahrooh 你能接受答案吗?
    • @Salman 好的接受,对不起,这个网站的新手
    猜你喜欢
    • 2017-10-25
    • 2011-01-01
    • 2019-05-05
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多