logical condition in vector operation

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

logical condition in vector operation

Federico Calboli
HI All,

I have a data frame such as:

> test
     x y  p  d
[1,] 1 0 10 21 0
[2,] 2 3 11 12 0
[3,] 3 4 12 23 0
[4,] 3 5 13 24 0


and I want to perfor some operations on the first two coulums,
conditional on the uneqaulity values on the 3rd and 4th columns.

For instance:

j = 3
test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]

gives me the result:

test:

     x y  p  d
[1,] 1 0 10 21 0
[2,] 2 3 11 12 0
[3,] 3 4 12 23 6
[4,] 3 5 13 24 7


My probblem is the following: I want to perform the operation
test[test[,1] == j,2] + test[test[,2] == j,1] only if the value of
column p and column d are different at the positions where x or y = j.
In practice, I don't want to perform the first operation because
test[2,4 is 12 and test[1,3] is 12 as well.

I tried an if statement with little success:

if(test[test[,1] == j,3] != test[test[,2] == j,4]){
test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]
}
Warning message:
the condition has length > 1 and only the first element will be used in:
if (test[test[, 1] == j, 3] != test[test[, 2] == j, 4]) {

Could anyone lend some advice?

Cheers,

Federico
--
Federico C. F. Calboli
Department of Epidemiology and Public Health
Imperial College, St Mary's Campus
Norfolk Place, London W2 1PG

Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193

f.calboli [.a.t] imperial.ac.uk
f.calboli [.a.t] gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: logical condition in vector operation

Christoph Buser
Dear Frederico

>From your example it is not clear to me what you like to obtain:
Please have a look on the slightly changed example here (I
changed two values to show a potentially undesired side effect of
your coding.


test <- data.frame(rbind(c(3,3,10,21,0), c(2,3,11,12,0), c(3,4,12,23,0),
                         c(3,5,13,24,0)))
names(test) <- c("x","y","p","d","su")
test

>>   x y  p  d su
>> 1 3 3 10 21  0
>> 2 2 3 11 12  0
>> 3 3 4 12 23  0
>> 4 3 5 13 24  0

j <- 3
test[test[,1] == j, 5] <- test[test[,1] == j,2] + test[test[,2] == j,1]

>> > > Warning message:
>> longer object length
>> is not a multiple of shorter object length in:
>>         test[test[, 1] == j, 2] + test[test[, 2] == j, 1]

Your code example produces now a warning for the adapted
data frame "test", since one tries to add two vectors of length 2
and 3, respectively. The result is based on recycling of the
smaller vector. In your example there was no warning since the
second column had only one entry.
The result with the adapted data frame is:

test
>>   x y  p  d su
>> 1 3 3 10 21  6
>> 2 2 3 11 12  0
>> 3 3 4 12 23  6
>> 4 3 5 13 24  8

Is that kind of recycling desired in your application. Otherwise
you should be careful with the coding example above.

Regards,

Christoph Buser

--------------------------------------------------------------
Christoph Buser <[hidden email]>
Seminar fuer Statistik, LEO C13
ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
phone: x-41-44-632-4673 fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------






Federico Calboli writes:
 > HI All,
 >
 > I have a data frame such as:
 >
 > > test
 >      x y  p  d
 > [1,] 1 0 10 21 0
 > [2,] 2 3 11 12 0
 > [3,] 3 4 12 23 0
 > [4,] 3 5 13 24 0
 >
 >
 > and I want to perfor some operations on the first two coulums,
 > conditional on the uneqaulity values on the 3rd and 4th columns.
 >
 > For instance:
 >
 > j = 3
 > test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]
 >
 > gives me the result:
 >
 > test:
 >
 >      x y  p  d
 > [1,] 1 0 10 21 0
 > [2,] 2 3 11 12 0
 > [3,] 3 4 12 23 6
 > [4,] 3 5 13 24 7
 >
 >
 > My probblem is the following: I want to perform the operation
 > test[test[,1] == j,2] + test[test[,2] == j,1] only if the value of
 > column p and column d are different at the positions where x or y = j.
 > In practice, I don't want to perform the first operation because
 > test[2,4 is 12 and test[1,3] is 12 as well.
 >
 > I tried an if statement with little success:
 >
 > if(test[test[,1] == j,3] != test[test[,2] == j,4]){
 > test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]
 > }
 > Warning message:
 > the condition has length > 1 and only the first element will be used in:
 > if (test[test[, 1] == j, 3] != test[test[, 2] == j, 4]) {
 >
 > Could anyone lend some advice?
 >
 > Cheers,
 >
 > Federico
 > --
 > Federico C. F. Calboli
 > Department of Epidemiology and Public Health
 > Imperial College, St Mary's Campus
 > Norfolk Place, London W2 1PG
 >
 > Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193
 >
 > f.calboli [.a.t] imperial.ac.uk
 > f.calboli [.a.t] gmail.com
 >
 > ______________________________________________
 > [hidden email] mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: logical condition in vector operation

Federico Calboli
On Thu, 2006-02-09 at 09:22 +0100, Christoph Buser wrote:

> Dear Frederico
>
> From your example it is not clear to me what you like to obtain:
> Please have a look on the slightly changed example here (I
> changed two values to show a potentially undesired side effect of
> your coding.
>
>
> test <- data.frame(rbind(c(3,3,10,21,0), c(2,3,11,12,0), c(3,4,12,23,0),
>                          c(3,5,13,24,0)))
> names(test) <- c("x","y","p","d","su")
> test
>
> >>   x y  p  d su
> >> 1 3 3 10 21  0
> >> 2 2 3 11 12  0
> >> 3 3 4 12 23  0
> >> 4 3 5 13 24  0
>
> j <- 3
> test[test[,1] == j, 5] <- test[test[,1] == j,2] + test[test[,2] == j,1]
>
> >> > > Warning message:
> >> longer object length
> >> is not a multiple of shorter object length in:
> >>         test[test[, 1] == j, 2] + test[test[, 2] == j, 1]
>
> Your code example produces now a warning for the adapted
> data frame "test", since one tries to add two vectors of length 2
> and 3, respectively. The result is based on recycling of the
> smaller vector. In your example there was no warning since the
> second column had only one entry.
> The result with the adapted data frame is:
>
> test
> >>   x y  p  d su
> >> 1 3 3 10 21  6
> >> 2 2 3 11 12  0
> >> 3 3 4 12 23  6
> >> 4 3 5 13 24  8
>
> Is that kind of recycling desired in your application. Otherwise
> you should be careful with the coding example above.

Recycling was and is integral part of my plan.

Cheers,

Federico

--
Federico C. F. Calboli
Department of Epidemiology and Public Health
Imperial College, St Mary's Campus
Norfolk Place, London W2 1PG

Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193

f.calboli [.a.t] imperial.ac.uk
f.calboli [.a.t] gmail.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: logical condition in vector operation

PIKAL Petr
In reply to this post by Federico Calboli
Hi

try
ifelse()

but I you probably shall put your problem clearer.

HTH
Petr


On 8 Feb 2006 at 18:12, Federico Calboli wrote:

From:           Federico Calboli <[hidden email]>
To:             r-help <[hidden email]>
Organization:   Imperial College London
Date sent:       Wed, 08 Feb 2006 18:12:37 +0000
Subject:         [R] logical condition in vector operation
Send reply to:   [hidden email]
        <mailto:[hidden email]?subject=unsubscribe>
        <mailto:[hidden email]?subject=subscribe>

> HI All,
>
> I have a data frame such as:
>
> > test
>      x y  p  d
> [1,] 1 0 10 21 0
> [2,] 2 3 11 12 0
> [3,] 3 4 12 23 0
> [4,] 3 5 13 24 0
>
>
> and I want to perfor some operations on the first two coulums,
> conditional on the uneqaulity values on the 3rd and 4th columns.
>
> For instance:
>
> j = 3
> test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]
>
> gives me the result:
>
> test:
>
>      x y  p  d
> [1,] 1 0 10 21 0
> [2,] 2 3 11 12 0
> [3,] 3 4 12 23 6
> [4,] 3 5 13 24 7
>
>
> My probblem is the following: I want to perform the operation
> test[test[,1] == j,2] + test[test[,2] == j,1] only if the value of
> column p and column d are different at the positions where x or y = j.
> In practice, I don't want to perform the first operation because
> test[2,4 is 12 and test[1,3] is 12 as well.
>
> I tried an if statement with little success:
>
> if(test[test[,1] == j,3] != test[test[,2] == j,4]){
> test[test[,1] == j, 5] = test[test[,1] == j,2] + test[test[,2] == j,1]
> } Warning message: the condition has length > 1 and only the first
> element will be used in: if (test[test[, 1] == j, 3] != test[test[, 2]
> == j, 4]) {
>
> Could anyone lend some advice?
>
> Cheers,
>
> Federico
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html