dimanche 13 septembre 2020

for loop and if statement gives wrong argument

I have the following code:

df1 <- data.frame(items=c("^TP53$","^PTEN","^BRACA1$","^SYNE4$","^ATM$"),condition = rnorm(5), 
                  parameter = rnorm(5), id = rnorm(5), reading = rnorm(5))
df1
listid<-gsub('[$|^]',"", df1$items)
protein<-c("PTEN")
fkeep <- NULL
for (i in 1:length(listid)) {
  if (!protein) {discard <- grep(paste0("^", listid[i], "$"), df1$items, fixed = T)}
  else {hits <- grep(paste0("^", listid[i]), df1$items, fixed = T)}
  fkeep <- c(fkeep, hits)
  # print(fkeep)
}

I have a data frame df1 and if a name in "listid" it is not in "protein" it get saved to discard , instead if it is in "protein" it is saved to hits and only hits are kept However I get the following error:

Error in !protein : invalid argument type 

Aucun commentaire:

Enregistrer un commentaire