Delete columns with almost specific name

I have a data d-frame and I want to delete columns that have almost the same name. Example col, col1, col2 .... coln

I tried something like this:

d$coln <- NULL 

but it only works for the last coln column.

+4
source share
1 answer

Use grep to identify columns and list(NULL) to remove them.

Some sample data:

 set.seed(1) mydf <- data.frame(id_1 = 1:6, id_2 = c("A", "B"), varA.1 = sample(letters, 6), varA.2 = sample(letters, 6), varA.3 = sample(letters, 6), varB.2 = sample(10, 6), varB.3 = sample(10, 6), varC.3 = rnorm(6)) mydf # id_1 id_2 varA.1 varA.2 varA.3 varB.2 varB.3 varC.3 # 1 1 A gyr 4 3 -0.04493361 # 2 2 B jqj 7 4 -0.01619026 # 3 3 A nps 8 1 0.94383621 # 4 4 B ubl 2 10 0.82122120 # 5 5 A eep 10 6 0.59390132 # 6 6 B sdu 1 2 0.91897737 

Let me remove all columns that have "varA" in it.

 mydf[grep("varA", names(mydf))] mydf[grep("varA", names(mydf))] <- list(NULL) mydf # id_1 id_2 varB.2 varB.3 varC.3 # 1 1 A 4 3 -0.04493361 # 2 2 B 7 4 -0.01619026 # 3 3 A 8 1 0.94383621 # 4 4 B 2 10 0.82122120 # 5 5 A 10 6 0.59390132 # 6 6 B 1 2 0.91897737 
+6
source

Source: https://habr.com/ru/post/1493823/


All Articles