melil
melil

Reputation: 81

ifelse() function combined with grep() function did not return the ifelse conditions

I want to apply a function such that if the column names of top.genes.matrix begins with non-tumor, its considered a Ctl sample. Otherwise, its Glioma sample. However, the code below returns everything as Ctl sample. Why?

type <- lapply(colnames(top.genes.matrix), 
               function(x) {
                 ifelse(grep("^non-tumor", x), "Ctl", "Glioma")
                 })
label  <- as.vector(unlist(type))

dput(top.genes.matrix[1:2,1:30]) structure(list(non-tumor_GSM97800 = c(9.7453140114e+288, 5.29888441356601e+246 ), non-tumor_GSM97803 = c(Inf, 3.67009936848685e+220), non-tumor_GSM97804 = c(Inf, 4.2899121663437e+41), non-tumor_GSM97805 = c(Inf, 8.40516277768061e+129 ), non-tumor_GSM97807 = c(Inf, 5.18689446110124e+179), non-tumor_GSM97809 = c(Inf, 1.25493857651029e+245), non-tumor_GSM97811 = c(Inf, 1.54683122103543e+213 ), non-tumor_GSM97812 = c(Inf, 1.1835373727435e+71), non-tumor_GSM97816 = c(Inf, 7.20674433073256e+130), non-tumor_GSM97817 = c(Inf, Inf), non-tumor_GSM97820 = c(Inf, 9.09145681001345e+174), non-tumor_GSM97825 = c(Inf, 4.71809784114593e+281 ), non-tumor_GSM97827 = c(Inf, 3.02115472858483e+210), non-tumor_GSM97828 = c(Inf, 1.58083541806485e+93), non-tumor_GSM97833 = c(Inf, 2.15709053730637e+182 ), non-tumor_GSM97834 = c(Inf, 4.12730102449738e+267), non-tumor_GSM97840 = c(Inf, 4.13431590005232e+162), non-tumor_GSM97846 = c(Inf, 4.22577864400639e+156 ), non-tumor_GSM97848 = c(Inf, 2.77631637353339e+211), non-tumor_GSM97849 = c(Inf, 2.24976436669632e+275), non-tumor_GSM97850 = c(Inf, 4.07675022923096e+131 ), non-tumor_GSM97853 = c(Inf, 3.22445392538858e+265), non-tumor_GSM97855 = c(Inf, 2.9272477355908e+244), astrocytomas_II_GSM97878 = c(6.99920231930564e+100, 2.71228730528275e+21), astrocytomas_II_GSM97913 = c(4.425834769824e+194, 5.12336586495199e+43), astrocytomas_II_GSM97932 = c(4.08905013671742e+108, 2581896.96873512), astrocytomas_II_GSM97939 = c(9.55556376729943e+116, 2.4150058542247e+56), astrocytomas_II_GSM97951 = c(Inf, 2.33290131037584e+113 ), astrocytomas_II_GSM97957 = c(3.32525456071435e+76, 2.03569771781862e+49 ), astrocytomas_II_GSM97972 = c(43317043134214.2, 500918273.092454 )), row.names = c("219752_at", "240512_x_at"), class = "data.frame")

label [1] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [13] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [25] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [37] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [49] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [61] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [73] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [85] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [97] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [109] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [121] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [133] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [145] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [157] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" [169] "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl" "Ctl"

Upvotes: 0

Views: 49

Answers (1)

forhad
forhad

Reputation: 196

grep returns only the matching position where as grepl returns the logical expression which you need. You can try it with more simpler way like this:

label <- ifelse(grepl("^non-tumor", colnames(top.genes.matrix)), "Ctl", "Glioma")

Upvotes: 0

Related Questions