[R] replace items in vector with character based on logical operator

Adrian Johnson oriolebaltimore at gmail.com
Sat Nov 12 23:25:51 CET 2016


Hi group,

I have a vector of numeric items and I want to replace based on
logical operator (> or <)  with 'up', 'down' or 'nochange'

The way I am doing works sometimes and does not work sometime. I dont
understand the problem. In this example, that works for condition DN
first, gets over-written by UP and then by NC.


 I appreciate your help.
Thanks
Adrian

In the example below (dput code given below).

qt = quantile(kx)
kx[kx <= qt[[2]]] <- 'DN'
kx[kx >=qt [[4]]] <- 'UP'
kx[kx < qt[[4]] | kx > qt[[2]]] <- 'NC'


> qt
       0%       25%       50%       75%      100%
 9.341531  9.995026 10.186305 10.444237 11.013304

> kx[kx <= qt[[2]]] <- 'DN'
> kx
      TCGA-3H-AB3K       TCGA-3H-AB3L       TCGA-3H-AB3M
TCGA-3H-AB3O       TCGA-3H-AB3S       TCGA-3H-AB3T       TCGA-3H-AB3U
     TCGA-3H-AB3X
"11.0133035117116"               "DN" "10.2976687932597"
"10.080008468093" "10.3661039885332"               "DN"
"10.8971723299346" "10.0327245097586"
      TCGA-3U-A98D       TCGA-3U-A98E       TCGA-3U-A98F
TCGA-3U-A98G       TCGA-3U-A98H       TCGA-3U-A98I       TCGA-3U-A98J
     TCGA-LK-A4NW
"10.0094206497379" "10.4816534666287" "10.4317651665439"
"DN" "10.2313364037012"               "DN" "10.1412738417844"
     "DN"
      TCGA-LK-A4NY       TCGA-LK-A4NZ       TCGA-LK-A4O0       TCGA-LK-A4O2
 "10.603941260297" "10.7959493941044" "10.0501324788234" "10.3877479259697"







kx <- structure(c(11.0133035117116, 9.95184228858716, 10.2976687932597,
10.080008468093, 10.3661039885332, 9.34153148077152, 10.8971723299346,
10.0327245097586, 10.0094206497379, 10.4816534666287, 10.4317651665439,
9.57711797674944, 10.2313364037012, 9.39562974544228, 10.1412738417844,
9.80706011783492, 10.603941260297, 10.7959493941044, 10.0501324788234,
10.3877479259697), .Names = c("TCGA-3H-AB3K", "TCGA-3H-AB3L",
"TCGA-3H-AB3M", "TCGA-3H-AB3O", "TCGA-3H-AB3S", "TCGA-3H-AB3T",
"TCGA-3H-AB3U", "TCGA-3H-AB3X", "TCGA-3U-A98D", "TCGA-3U-A98E",
"TCGA-3U-A98F", "TCGA-3U-A98G", "TCGA-3U-A98H", "TCGA-3U-A98I",
"TCGA-3U-A98J", "TCGA-LK-A4NW", "TCGA-LK-A4NY", "TCGA-LK-A4NZ",
"TCGA-LK-A4O0", "TCGA-LK-A4O2"))



More information about the R-help mailing list