Skip to content

array manipulation simple questions

4 messages · Λεωνίδας Μπαντής, jdeisenberg, Gabor Grothendieck +1 more

#
???????? ??????? wrote:
I think you mean >=5, not > 5.  In that case, try this:

b <- ifelse( a < 5, 0, 1)
???????? ??????? wrote:
Not sure how to insert the 4,5, but this will find the where the 2's are:
which(a == 2)
#
2009/2/23 ???????? ??????? <bleonidas25 at yahoo.gr>:
(a>5)+0

# or

out <- as.numeric(a > 5)
if (is.matrix(a)) out <- matrix(a, 1)
out
# return c(4, 5, 2) or x
f <- function(x) if (x==2) c(4:5, x) else x

# apply f to each element and
# if a is a matrix make output a matrix too

out <- unlist(sapply(a, f))
if (is.matrix(a)) out <- matrix(out, nrow(a))
out
which(a == 2)
#
On Feb 24, 3:57?pm, jdeisenberg <catc... at catcode.com> wrote:
you could also use

b <- 1*(x >= 5) #the logical elements get converted to numeric
you could use

b <- rep(a,times = 2*(a==2) + 1)
replace(b, which(b==2), c(4,5,2))