Teres
Teres

Reputation: 73

Removing columns from a dataframe with a common string but keeping the first

In my dataframe frame I have columns with names like this:

stockA_1,stockA_2,stockA_3,stockA_4

I would like to delete all rows from my df which have the common string "stockA_" but I would like to keep only the first column with this synthetic "stockA_1"

How is it possible to make it?

Upvotes: 1

Views: 63

Answers (2)

jogo
jogo

Reputation: 12559

This can be done with base functions:

d <- as.data.frame(matrix(1:35, 5, 7))
names(d) <- c("AA", "stockA_1", "BBB", "stockA_2", "stockA_3", "CCCCC", "stockA_4")
d[,-which(grepl("^stockA_", names(d)))[-1]]

The result is:

> d[,-which(grepl("^stockA_", names(d)))[-1]]
  AA stockA_1 BBB CCCCC
1  1        6  11    26
2  2        7  12    27
3  3        8  13    28
4  4        9  14    29
5  5       10  15    30

If you want to conserve the column "stockA_1" (which is eventually not in the first place under the "stockA_"-columns) then you can do

d <- as.data.frame(matrix(1:35, 5, 7))
names(d) <- c("AA", "stockA_11", "BBB", "stockA_2", "stockA_1", "CCCCC", "stockA_4")
i <- (!grepl("^stockA_", names(d))) | grepl("^stockA_1$", names(d))
d[,i]

with the result:

> d[,i]
  AA BBB stockA_1 CCCCC
1  1  11       21    26
2  2  12       22    27
3  3  13       23    28
4  4  14       24    29
5  5  15       25    30

Upvotes: 2

User2321
User2321

Reputation: 3062

Using the data table (if I have understood the problem correctly that is):

require(data.table)
data <- data.table(stockA_1 = c(1, 2, 3), stockA_2 = c(3, 4, 7), stockA_3 = c(4, 5, 6))

columns <- setdiff(grep("stockA_", names(data)), grep("stockA_1", names(data)))
data[, (columns):= NULL]

Upvotes: 0

Related Questions