I have a dataframe consisting of 271 columns. Each column is named as "sampleName_time".
[1] "A01_0" "A01_24" "A01_48" "A01_72" "A02_48" "A03_0" "A03_24" "A03_48" "A04_24" "A04_48" "A05_0" "A05_24" "A05_48"
[14] "A05_72" "A06_0" "A06_48" "A07_0" "A07_24" "A07_48" "A08_0" "A08_24" "A08_48" "A08_72" "A09_24" "A09_48" "A09_72"
[27] "A10_0" "A10_24" "A10_48" "A11_0" "A11_48" "A11_72" "A12_48" "B01_0" "B01_24" "B01_48" "B01_72" "B02_24" "B02_48"
...
The time can be 0, 24, 48 or 72. I want to keep the columns that refer to the same sample and have measurements on all time points (0, 24, 48 and 72). For example, The sample A01 is ok because we have columns "A01_0", "A01_24", "A01_48" and "A01_72". A02 is not ok because there is only one column named "A02_48" but no others. A03 is not ok too. But A05 is ok. So for the above example, the pruned dataframe I want would be as follows
"A01_0" "A01_24" "A01_48" "A01_72" "A05_0" "A05_24" "A05_48"
"A05_72" "A08_0" "A08_24" "A08_48" "A08_72" "B01_0" "B01_24" "B01_48" "B01_72"
...
The following is my approach. But it seems so complicated as I need to have the nested if statements and maybe I need to use next to skip some iterations of the for loop.
for (i in seq_along(colnames(exprs))){
if (tr_split(colnames(exprs)[i], "_")[1][2] == 0){
if (tr_split(colnames(exprs)[i+1], "_")[1][2] == 24){
# not complete with more if statements
}
}
}
Are there some neat ways to do that?
I think there may be some neat way using sapply(colnames(exprs),function(x){ someFunction})
Thanks