This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to combine seperate seurat meta.data ID;s into one column?

I have an integrated object with 4 data sets (Veh1, Exp1, Veh2, Exp2)

These tags are appended in the object meta data under object$cond

What I would like to do is create a new metadata column called "exp", and label any cell that was labeled as Veh1 or Veh2 as VEH and any cell that was labeled as Exp1/Exp2 as EXP so that it would look something like this when finished:

              orig.ident    cond     seurat_clusters   exp
Veh1_AAACCTGAGATCCTGT       VEH1        6              VEH
Veh2_AAACCTGCAGTACACT       VEH2        9              VEH
Exp1_AAACCTGGTCCCTTGT       EXP1        12             EXP
Exp2_AAACCTGTCAAAGACA       EXP2        7              EXP

I have tried using 4 seperate if statements for each condition:

if (object$cond == "VEH1") {
  object$exp <- "VEH"
}

and I have tried using an ifelse statement like this:

if (object$cond == "VEH1") { 
  object$exp <- "VEH"
} else if (object$cond == "VEH2") {
  object$exp <- "VEH"
} else if  (object$cond == "EXP1") {
  object$exp <- "EXP"
} else {object$cond == "EXP2"
  object$exp <- "EXP"
}

both of these labeled every cell as VEH, and I got the warning

Warning message:
In if (object$cond == "VEH") { :
  the condition has length > 1 and only the first element will be used
> head(x = object[[]])

lastly I tried using a for loop like this, where 48,000 is the number of cells I have

for (i in 1:48000)
{
  if (object$cond[[i]] == "VEH1") {
    object$exp <- "VEH"
  }
}

This didn't work etihe.

Does anyone have a recommendation? I fell like this simple task shouldn't be that difficult but I can't figure out how to get it

Thanks in advance!

seurat scrna

1 answer

It looks like you're trying to add a column to a data.frame. You can simply use something like:

df$new_col <- function_to_manipulate(df$existing_col)

where function_to_manipulate() is any function that gets new values from old values (which in your case could be a gsub or a toupper(substr())).

You should be able to use the clues above to figure out exact code.

Log in to answer this question.