Create new columns and change names

I have some data from a FACS (fluorescent activated cell sorting) experiment that I want to analyze using R. The problem is that the FACS software has a weird output and basically puts all the characteristics in one column with a "-" in between them.
clone 9_+b_004.fcs/Single cells/red vs. green
clone 9_+T_004.fcs/Single cells/red vs. green
clone 10_+b_004.fcs/Single cells/red vs. green
and so on

What I need for my analysis are new columns. For example a column named "Clone" with a variable "clone 9" if there is clone 9 in the original column, or "clone 10" if there is clone 10 in the original column. And also a column "Added compound" with variables such as "TGFb" if there's "+b" in the original column, and "Tamoxifen" if there is "+T" etc...

The problem is that I have no idea how to create those columns. I've tried several different codes and combinations (grepl, replace, mutate, to name a few) but nothing has worked so far. Because I need multiple new columns, I can't just replace the original column without duplicating.

Any help or ideas are much appreciated.

You can do something like this


sample_df <- data.frame(
    stringsAsFactors = FALSE,
    original_column = c(
        "clone 9_+b_004.fcs/Single cells/red vs. green",
        "clone 9_+T_004.fcs/Single cells/red vs. green",
        "clone 10_+b_004.fcs/Single cells/red vs. green"

sample_df %>% 
             into = c("clone", "added_compound", "other_column"),
             sep = "_") %>% 
    mutate(added_compound = case_when(
        added_compound == "+b" ~ "TGFb",
        added_compound == "+T" ~ "Tamoxifen"
#>      clone added_compound                       other_column
#> 1  clone 9           TGFb 004.fcs/Single cells/red vs. green
#> 2  clone 9      Tamoxifen 004.fcs/Single cells/red vs. green
#> 3 clone 10           TGFb 004.fcs/Single cells/red vs. green

Created on 2020-03-08 by the reprex package (v0.3.0.9001)

If this doesn't solve your issue, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Hi @DaphneLM, if you could post a small sample of your data (a table of at most 50 rows, and maybe 6-10 columns), that would be very helpful -- is your data in a data frame?

Thank you for your answer! This solved my problem perfectly. It hadn't occurred to me to use the separate in combination with mutate. Thank you!

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.