How to combine seperate seurat meta.data ID;s into one column?
1
0
Entering edit mode
4.4 years ago
cook.675 ▴ 220

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 • 5.9k views
ADD COMMENT
0
Entering edit mode
4.4 years ago
Ram 43k

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.

ADD COMMENT
0
Entering edit mode

Thanks for the hint. I tried:

object$exp <- AddMetaData(object, object$cond == "VEH1", col.name = "VEH")

and got returned an error.

But I found the solution here!!

ADD REPLY

Login before adding your answer.

Traffic: 2815 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6