Adrian Johnson
2016-Nov-12 22:25 UTC
[R] replace items in vector with character based on logical operator
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'> qt0% 25% 50% 75% 100% 9.341531 9.995026 10.186305 10.444237 11.013304> kx[kx <= qt[[2]]] <- 'DN' > kxTCGA-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"))
Jeff Newmiller
2016-Nov-12 22:51 UTC
[R] replace items in vector with character based on logical operator
Your goal is fundamentally flawed, since all elements of a vector must be if the same type. Create another vector to hold your character information. kc <- rep( 'NC', length( kx ) ) kc[ kx <= qt[[2]] ] <- 'DN' kc[ kx >=qt [[4]] ] <- 'UP' -- Sent from my phone. Please excuse my brevity. On November 12, 2016 2:25:51 PM PST, Adrian Johnson <oriolebaltimore at gmail.com> wrote:>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")) > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2016-Nov-12 23:29 UTC
[R] replace items in vector with character based on logical operator
Your first replacement in kx changed it to a character vector and comparisons of character strings given different results than comparisons of numbers> kx <- 1:10 > qt <- quantile(kx) > kx[kx <= qt[2]] <- "DN" > kx[1] "DN" "DN" "DN" "4" "5" "6" "7" "8" "9" "10"> "10" < "6"[1] TRUE> > # compare to > kx <- 1:10 > code <- character(length(kx)) > qt <- quantile(kx) > code[kx <= qt[2]] <- "DN" > code[1] "DN" "DN" "DN" "" "" "" "" "" "" ""> code[kx >= qt[4]] <- "DN" > code[1] "DN" "DN" "DN" "" "" "" "" "DN" "DN" "DN" Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Nov 12, 2016 at 2:25 PM, Adrian Johnson <oriolebaltimore at gmail.com> wrote:> 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")) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]