Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an R function to make subset by part of column name?

Tags:

dataframe

r

I'm looking for a function in order to split a dataframe in several dataframe by the end of column names. To take an example :

Year | hour | LOT | S123_AA | S135_AA | S1763_BB | S173_BB | ...

So I want to split it into 2 dataframes as:

Year | hour | LOT | S123_AA | S135_AA |

and

Year | hour | LOT | S1763_BB | S173_BB |

My key point is to conserve the first 3 columns and append all columns where the end names is _AA and _BB.

Thanks for your time

like image 752
Alex Germain Avatar asked Dec 30 '18 16:12

Alex Germain


People also ask

How do I subset data based on column names in R?

3.1 Subset by Column Name Let's use the same df[] notation and subset() function to subset the data frame by column name in R. To subset columns use select argument with values as column names to subset() .

Which functions can be used to get subset columns using their names?

Your answer You can use grepl on the names of data frame.


1 Answers

You can just take the right subset using grep.

df_AA = df[,c(1:3, grep("_AA$", colnames(df)))]
df_BB = df[,c(1:3, grep("_BB$", colnames(df)))]
like image 118
G5W Avatar answered Nov 06 '22 00:11

G5W