technical problem with R and GO terms
1
0
Entering edit mode
6.9 years ago
lessismore ★ 1.4k

Hey all, i have a technical problem. i have a 2 column table with a list of GO and respective p-value in R. Now i want to add another column based on a specific pattern in the GO term. e.g. if the GO term starts with GO:002* (for capturing e.g. GO:002545545,GO:002434343, GO:00254457,etc..) i want to add "yes" to the third column, otherwise "no". Does someone know how to do this in R? thanks in advance

R GO • 1.7k views
ADD COMMENT
0
Entering edit mode

I dont understand exactly the pattern but following is the code snippet: add_col <- c() df #data.frame containing the two columns for(i in df[,1]) { if(i matches pattern) add_col <- c(add_col, 'yes') else add_col <- c(add_col, 'no') } cbind(df, add_col)

If pattern is clear I can perhaps write a more proper if.

ADD REPLY
3
Entering edit mode
6.9 years ago
ATpoint 86k

Given that you have a dataframe termed df which has two columns, use grepl() to find your pattern:

# add a blank third column
df[,3] <- NA

# fill "yes" if column 1 contains the pattern:
df[grepl("GO:002", df[,1]),3] <- c("yes")

# and "no" if not => simple use !grepl() as the opposite to grepl() :
df[!grepl("GO:002", df[,1]),3] <- c("no")
ADD COMMENT
0
Entering edit mode

thanks a lot for your answer! do you think is possible to embed these 3 operations in a function and apply this function to a list of n dataframes?

ADD REPLY
2
Entering edit mode

Yep, something like:

findreplace<-function(df)
{
  df[,3] <- NA
  df[grepl("GO:002", df[,1]),3] <- c("yes")
  df[!grepl("GO:002", df[,1]),3] <- c("no")
  df
}

list_of_df <- list(df1,df2,df3)
fixed_list <- lapply(list_of_df,findreplace)
ADD REPLY

Login before adding your answer.

Traffic: 1210 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