Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772, -1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413, 1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
Hi Adrian I'm not sure that you need to use the ifelse here. You can simply
assign values ina vector or matrix using a simple condition -- here is a simple
example:
v<-c(4,5,6,7)
v1<-v
v1[]<-0
v1[v<5]<--1
v1[v>6]<-1
v1
Nick
On 22 May 2016 at 18:58 Adrian Johnson <oriolebaltimore at gmail.com> wrote:
Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
> k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
> k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577,
-0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
> k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
On Sun, May 22, 2016 at 2:00 PM Adrian Johnson <oriolebaltimore at gmail.com>
wrote:
Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
Thank you both Dylan and Wray.
since my matrix is quite large and for simplicity in downstream
operation, i will use sign function. thanks a lot.
On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan <dylan.keenan at gmail.com> wrote:
Try this:
sign(ifelse(abs(k)<=1.5, 0, k))
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
On Sun, May 22, 2016 at 2:00 PM Adrian Johnson <oriolebaltimore at gmail.com>
wrote:
Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577,
-0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
On May 22, 2016, at 11:23 AM, Adrian Johnson <oriolebaltimore at gmail.com> wrote:
Thank you both Dylan and Wray.
since my matrix is quite large and for simplicity in downstream
operation, i will use sign function. thanks a lot.
On Sun, May 22, 2016 at 2:12 PM, Dylan Keenan <dylan.keenan at gmail.com> wrote:
Try this:
sign(ifelse(abs(k)<=1.5, 0, k))
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
If the problems were somewhat less symmetric or more complex this would be a method that could be easily generalized to a larger number of less "absolutely" symmetric intervals:
k2 <- k
k2[] <- findInterval(k2, c(-Inf, -1.5, 1.5, Inf) ) -2
# shifts the 1-3 values to -1 to 1
k2
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
Using k2[] <- ... preserves the matrix structure
On Sun, May 22, 2016 at 2:00 PM Adrian Johnson <oriolebaltimore at gmail.com>
wrote:
Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577,
-0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Sun, May 22, 2016 at 1:58 PM, Adrian Johnson <oriolebaltimore at gmail.com>
wrote:
Hi group:
I am having difficulty with if else condition. I kindly request some help.
I have a matrix k
k
C1 C2 C3 C4
A 0.09902175 -0.1083887 0.2018689 -0.3546167
B 1.60623838 -1.4167034 0.9076373 -0.3161138
C -0.10433133 -1.7060911 -0.4030050 1.0153297
D -2.91485614 2.9201895 -2.4771802 -2.6991517
I want to convert values > 1.5 to 1, < -1.5 to -1 and rest to 0;
k1 - desired output
C1 C2 C3 C4
A 0 0 0 0
B 1 0 0 0
C 0 -1 0 0
D -1 1 -1 -1
I am trying with if else but cannot do it. I could only define one
condition. Could someone help how I can do this. I dont mean only if
else, but any other way.
k =
structure(c(0.0990217544905328, 1.60623837694539, -0.104331330281166,
-2.91485614212114, -0.108388742328104, -1.41670341534772,
-1.70609114096417,
2.92018951284015, 0.201868946570178, 0.907637296638577, -0.403004972105994,
-2.47718015803221, -0.354616729237253, -0.316113789733413,
1.01532974064126,
-2.69915170731852), .Dim = c(4L, 4L), .Dimnames = list(c("A",
"B", "C", "D"), c("C1", "C2", "C3", "C4")))
k1 <- t(apply(k, 1, function(x) ifelse(x > 1.5,1,-1)))
k1
C1 C2 C3 C4
A -1 -1 -1 -1
B 1 -1 -1 -1
C -1 -1 -1 -1
D -1 1 -1 -1
Thanks
Adrian
jim holtman <jholtman at gmail.com>
on Sun, 22 May 2016 16:47:06 -0400 writes:
> if you want to use 'ifelse', here is a way:
hmm, why should he want that ?
The OP did mention that it's about somewhat large objects, so
efficiency is one of the considerations :
ifelse() is often convenient and nicely self-explaining, but it
is (because of its generality, but also by its definition)
much *less efficient* than the (sometimes slightly less
convenient) ways you were shown previously in this thread :
- For the generalized case findInterval() is order of magnitudes
better, and
- for the simple case you were shown to use logical indexing,
i.e., calls ? la x[x > k] <- ..
In summary:
Use ifelse() much less -- notably if writing
functions/code which should scale !
Martin Maechler
ETH Zurich