[R] delete columns which partially match expression

jim holtman jholtman at gmail.com
Thu Oct 13 15:23:56 CEST 2011


try this:

> x <- read.table(textConnection("A             B             C             D             E
+ 12           33           Error1   71           Error2
+ 12           33           Error1   71           Error2
+ 12           33           Error1   71           Error2
+ 12           33           Error1   71           Error2
+ 12           33           Error1   71           Error2")
+     , as.is = TRUE
+     , header = TRUE
+     )
> closeAllConnections()
> colMatch <- which(apply(x, 2, function(a) any(grepl("Error", a))))
> colMatch
C E
3 5
> # delete columns
> x[, -colMatch]
   A  B  D
1 12 33 71
2 12 33 71
3 12 33 71
4 12 33 71
5 12 33 71
>


On Thu, Oct 13, 2011 at 9:10 AM, Samir Benzerfa <benzerfa at gmx.ch> wrote:
> Hello everyone,
>
>
>
> I'd like to search for certain "expressions" (characters) in my data.frame
> and delete the containing columns.
>
>
>
> So, for example in the below table, I'd like to delete all columns which
> contain the expression "Error". That is, R should delete column C and E from
> my data. Any ideas?
>
>
>
> A             B             C             D             E
>
> 12           33           Error1   71           Error2
>
>
>
> Cheers, S.B.
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list