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
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() .
Your answer You can use grepl on the names of data frame.
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)))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With