Selecting non-consecutive columns in R tables

前端 未结 4 1478
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  感情败类
    2021-02-01 04:46

    You simply first generate the indexes you want. The c function allows you to concatenate values. The values can be either column indices or column names (but not mixed).

    df <- data.frame(matrix(runif(100), 10))
    cols <- c(1, 4:8, 10)
    df[,cols]
    

    You can also select which column indices to remove by specifying a negative index:

    df[, -c(3, 5)] # all but the third and fifth columns
    

提交回复
热议问题