2

I have the following data frame in R

> head(genes)
     Genes
1  APOBEC4
2  B3GALT2
3 C1orf127
4   CAMK1G
5   CAMTA1
6    EPHX1

I have a vector with row indexes.

> head(sig)
[1]  10  11  41  54 125 126

Now I want to add another columns to data frame genes, such that the rows at the index sig will get a values "Yes" and otherwise "NO"

Something like:

head(genes)
     Genes Sig
1  APOBEC4 NO
2  B3GALT2 NO
3 C1orf127 NO
4   CAMK1G NO
5   CAMTA1 NO
...
10  XXX    YES
...
41  YYY    YES

I tried but I could not find a solution.

1
  • I do not understand why people gives negative votes. Everyone is not expert on everything and may not have a basic understanding of other programming languages. I write python snippets and shell scripts, I analyze complex genomics data to understand even more complex biology behind it. Suddenly, If I ask a basic question in something where I do not have a clue, and its a bit urgent, people gets annoyed ? Cool. Commented Jan 9, 2017 at 11:22

3 Answers 3

2

We can simply use base R

genes$Sig <-  c("NO", "YES")[(seq_len(nrow(genes)) %in% sig) + 1]

Or with ifelse

genes$Sig <- ifelse(seq_len(nrow(genes)) %in% sig, "YES", "NO")

Or create a column with "NO" and then assign "YES" based on the index

genes$Sig <- "NO"
genes$Sig[sig] <- "YES"
Sign up to request clarification or add additional context in comments.

Comments

2

I am not sure, if you have rownames in your dataframe. We can use row_number() function in dplyr to check if the row is present in sig and assign the value accordingly

library(dplyr)
genes %>%
    mutate(Sig = ifelse(row_number() %in% sig, "YES", "NO"))

Comments

0

Try something like this:

library(dplyr)
genes <- genes %>%
    mutate(Sig = case_when(as.numeric(row.names(.)) %in% sig ~ "YES", TRUE ~ "NO"))

I used it like this and it worked for me.

SAC_FULL_MERGE <- SAC_FULL_MERGE %>% 
     mutate(Country = 
     case_when(as.numeric(row.names(.)) <= 135159 ~ "US", 
               as.numeric(row.names(.)) > 135159 ~ "CAN")) %>% 
     as.data.frame()

1 Comment

This could also be simplified a bit by using if_else (or ifelse from base R) instead of case_when: Country = if_else(as.numeric(row.names(.)) <= 135159, "US", "CAN")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.