Teres Teres - 1 month ago 5
R Question

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?

Answer

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
Comments