Selecting non-consecutive columns in R tables

前端 未结 4 1469
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 04:19

Let\'s say I have a some table, T. Assume T has 5 columns. I understand how to select any consecutive subset of columns and store them as a new table. For that I would use brack

4条回答
  •  -上瘾入骨i
    2021-02-01 04:55

    For random columns check out ?sample

    df <- data.frame(matrix(runif(25), 5))
    df
    #         X1        X2         X3         X4        X5
    #1 0.7973941 0.6142358 0.07211461 0.01478683 0.6623704
    #2 0.8992845 0.8347466 0.54495115 0.52242817 0.4944838
    #3 0.8695551 0.9228987 0.00838420 0.58049324 0.9256282
    #4 0.1559048 0.7116077 0.08964883 0.06799828 0.3752833
    #5 0.2179599 0.4533054 0.60817319 0.62235228 0.8357441
    
    df[ ,sample(names(df), 3)]
    #         X5         X3        X2
    #1 0.6623704 0.07211461 0.6142358
    #2 0.4944838 0.54495115 0.8347466
    #3 0.9256282 0.00838420 0.9228987
    #4 0.3752833 0.08964883 0.7116077
    #5 0.8357441 0.60817319 0.4533054
    

提交回复
热议问题