How can you remove the over-representation caused by a group of majority columns?

Viewed 22

I'm currently working on a set of genetic data with two groups. P[Anynumber], P[Anynumber].PE

These groups are patients in different stages (P=Initial, PE = More than 24 weeks) Right now I have a larger group of P than PE. Around 96 samples of P, 65 of PE in the same dataframe

I already create a function to plot a PCA with both stages, but this over-representation caused by P group move all the PCA.

For that reason, what I would like to do is remove those that are not duplicates. I mean, I just want a dataframe that contain both samples. For example: P2 and PE_2, P3 and PE_3, and remove those who have P[AnyNumber] but not PE

Example dataframe:

  data <- data.frame(matrix(rbinom(10*1000, 1, .5), ncol=10))
  colnames(data) <- c("P1","P1.PE","P10","P100","P101","P101.PE", "P102", "P102.PE","P103","P104")

What I expect it would be a dataframe with the columns:

P1 | P1.PE | P101 | P101.PE | P102 | P102.PE |

What I tried to do is filter the dataframe using grepl firstly and strsplit, but I got and error and probably there's an easier way to do it.

data.filter <- data[,c(unlist(data.frame(strsplit(colnames(data[,grepl("\\.",colnames(data))]),"\\."),stringsAsFactors = F)[1,]),colnames(data[,grepl("\\.",colnames(data))]))]

Thank you in advance.

1 Answers

Perhaps we can remove the substring starting from . in the column name, apply duplicated get all the duplicated column names.

v1 <- sub("\\..*", "", names(data))
newdata <- data[duplicated(v1)|duplicated(v1, fromLast = TRUE)]
names(newdata)
#[1] "P1"      "P1.PE"   "P101"    "P101.PE" "P102"    "P102.PE"
Related