function - Preparing data set for volcano plot in R -


i have following dummy data set:

mydata = data.frame(fruit = c("apple", "apple", "apple", "apple", "apple", "apple", "apple", "pear", "pear", "pear", "pear", "pear", "pear", "lemon", "lemon", "lemon", "lemon", "lemon", "orange", "orange", "orange", "orange", "plum", "plum", "plum", "plum"), p = c(0.013, 0.018, 0.022, 0.035, 0.001, 0.030, 0.046, 0.031, 0.010, 0.017, 0.035, 0.054, 0.038, 0.038, 0.038, 0.036, 0.042, 0.043, 0.056, 0.062, 0.055, 0.031, 0.023, 0.003, 0.013, 0.009), f = c(3.4, 5.5, 4.4, 3.9, 3.7, 3.0, 1.5, 1.3, 2.4, 1.1, 3.6, 1.4, 1.5, 3.3, 2.0, 1.5, 1.4, 2.1, 4.0, 2.2, 1.7, 3.2, 4.9, 4.4, 2.1, 1.2)) 

(a) add column "t". value in each cell of "t" based on values in "p" , "f":

if p<0.05 , f>2 write content of corresponding cell under "fruit", else write "ns".

(this easy guys can't head wrapped around writing functions)

(b) add column "top". content of each cell in column "top" depends on how many times fruit occurs in column "t". i'm interested in keeping 2 abundant fruits found in "t" ("ns" not considered fruit).

if fruit in cell of "t" 1 of 2 abundant fruits in of "t" write fruit name corresponding cell of "top", else write "other". if cell of "t" contains "ns" write "ns" "top".

background:
using real data set create volcano plot (in ggplot2) , color-code "fruits" pass threshold. color-coding therefore based on information in column "t".
i running out of legend space , colors when create plot since have hundreds of "fruits". therefore color-code top 10 "fruits" pass thresholds , group remaining "fruits" pass thresholds under "others".

solved! part (a) solved baptiste's script. part (b) solved combining baptiste's script , jbaums' script:

mydata = transform(mydata, top = ifelse(t == "ns", "ns", ifelse(t %in% names(sort(table(t), dec=t))[names(sort(table(t), dec=t))!="ns"][1:2], levels(t)[t], "other"))) 

thanks guys!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -