3

Data frame A exists.

I want to create Data frame B and insert certain columns from data frame A in Data frame B.

I do not want to use the column numbers but the column names to do that

Thank you very much!!!!

aynber
  • 22,380
  • 8
  • 50
  • 63
Yaron Nolan
  • 153
  • 1
  • 1
  • 8
  • 2
    `insert certain columns` Those certain columns have to be either identified by index or the actual column names itself `dataB <- dataA[, c("col1", "col3", "col5")]` – akrun Dec 12 '19 at 14:32

1 Answers1

4

We can use the subset of column names if there are no patterns

dataB <- dataA[, c("P1", "xyz", "acdc")]

Or if there are some sequence of column names based on index, subset the column names with a position index and use that to select the columns

dataB <- dataA[, colnames(dataA)[c(1,2,4,5,6,7,8,9,10,40,43,46,47,48,49)]]

To make this easier, all the sequence can be abbreviated with :

dataB <- dataA[, colnames(dataA)[c(1:2, 4:10, 40, 43, 46:49)]]
akrun
  • 874,273
  • 37
  • 540
  • 662