samedi 13 juillet 2019

How do I extract elements from a dataframe by pattern?

I have a dataframe dat() that has many variables like "x_tp1_y" "g_tp1_z" "f_tp2_h".

I would like to extract elements that include "tp1".

I already tried this:

grep("tp1", dat)

grepl("tp1", dat)

dat["tp1",]

I just want R to give me elements with this pattern so I do not have to type in all variable names that are in the dataframe dat(). And then I would like to create a new dataframe.

(I know that I just can use newdat<- data.frame( dat[[1]], dat[ c(1:30)]) but I have so many elements in my dataframe that this would take ages.

Thank you for your help!

Aucun commentaire:

Enregistrer un commentaire