Re: [R] problem with ifelse

2012-05-31 Thread Sarah Goslee
Hi,

On Thu, May 31, 2012 at 12:49 PM, Christopher Kelvin
 wrote:
> Hello,
> Sorry for reply you directly but i feel that if i send to R they might not 
> post because they may not understand what i am trying to say
> I want to provide you with the code below by using the survival package, that 
> might make sense to you than all these codes i am confusing myself with.

But treating me as your personal R guru is a less-optimal solution
than asking the entire list. Someone else might understand your
problem; I certainly don't.

> I have say some values represented by r which i record for the first time i 
> am conducting my study for each individual, 15 people
> i have another set of data that i refer to as the stop time of my study 
> represented by t. Now what i have is two set of data say [1 2], [3 4] that is 
> numeric values.

So you have actual measured data for start time and stop time? What do
those look like?

> To get my desired output i require that the number of start time be equal to 
> the number of end time.

That seems unlikely.

> So at the end i need a data that has lower values in the left and upper 
> values in the right.

What do your recorded data look like, and what does your intended
output look like?

>
> library(survival)
> p1<-1.2;b<-1.5;n<-15
> r<-runif(n,min=0,max=b)
> t<-rweibull(n,shape=p1,scale=b)
> w=Surv(r,t+r,type="interval2")

So r and t+r are equivalent to your recorded data, and w is your
desired output? Comments would be helpful. Then why can't you use this
method to process your own data?

Sarah

>
> Thank you
> Chris
>
>
>
> - Original Message -
> From: Sarah Goslee 
> To: Christopher Kelvin ; r-help 
> 
> Cc:
> Sent: Thursday, May 31, 2012 11:19 PM
> Subject: Re: [R] problem with ifelse
>
> Hi,
>
> On Thu, May 31, 2012 at 10:09 AM, Christopher Kelvin
>  wrote:
>> Hello Sarah,
>>  I hope i have understood you; All i seek to do is to get a code that i can 
>> obtain interval censoring from without using the survival package. Can you 
>> come to my aid?
>
> Probably, but you need to meet me halfway.
>
> What do your inputs look like?
>
> What do your desired outputs look like?
>
> First, state them in plain English: my input is a vector of numeric
> values. In my desired output, this sort of value is changed to this
> number.
>
> Then provide that in R form. Here's an example input. Here's what I
> want the output to look like.
>
> I'm good at writing R code, but I'm not interested in wading through
> your non-working code to figure out what you meant. Please read the
> posting guide, and please send your replies back to the whole list and
> not just me.
>
> Sarah
>
>> Thank you
>> Chris
>>
>>
>>
>>
>>
>>
>>
>> - Original Message -
>> From: Sarah Goslee 
>> To: Christopher Kelvin ; r-help 
>> 
>> Cc:
>> Sent: Thursday, May 31, 2012 3:51 AM
>> Subject: Re: [R] problem with ifelse
>>
>> Since your code has things like this:
>> z<-numeric(length(t ((
>>
>> either you have a serious problem with your email client or you need
>> to reread some introductory material and take a hard look at your
>> code.
>>
>> Also note that g() doesn't work, because it contains the statement
>> return(m) but m is undefined within g().
>>
>> Meanwhile, you could provide what I asked: a statement of what you
>> expect your code to produce given particular input. Otherwise how
>> would we know if we've offered the right solution, since your function
>> doesn't work? Using set.seed() would be a useful component of this
>> reproducible example.
>>
>> Without having a working if poorly-written function to go by or a
>> clear results statement, I'm not interested in trying to rewrite your
>> code. But some thoughts:
>>
>> Here's a new version of f().
>>
>> f2 <- function(c1, c2) {
>>     r <- pmax(c1 + c2, c1 + 0.5)
>>     cbind(c1, r)
>> }
>>
>> It looks like you expected f() to be able to take vectors, but in g()
>> you only return one value. Is that a mistake, or what you wanted?
>> Since you're also using cbind(), I assume it's a mistake.
>>
>> Again, there are lots of problems here that suggest that you are
>> coming from some other programming language and have not taken the
>> time to learn much about R's syntax. This is easily remedied by
>> reading the introduction.
>>
>> Sarah
>>
>> On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin
>>

Re: [R] problem with ifelse

2012-05-31 Thread Sarah Goslee
Hi,

On Thu, May 31, 2012 at 10:09 AM, Christopher Kelvin
 wrote:
> Hello Sarah,
>  I hope i have understood you; All i seek to do is to get a code that i can 
> obtain interval censoring from without using the survival package. Can you 
> come to my aid?

Probably, but you need to meet me halfway.

What do your inputs look like?

What do your desired outputs look like?

First, state them in plain English: my input is a vector of numeric
values. In my desired output, this sort of value is changed to this
number.

Then provide that in R form. Here's an example input. Here's what I
want the output to look like.

I'm good at writing R code, but I'm not interested in wading through
your non-working code to figure out what you meant. Please read the
posting guide, and please send your replies back to the whole list and
not just me.

Sarah

> Thank you
> Chris
>
>
>
>
>
>
>
> - Original Message -
> From: Sarah Goslee 
> To: Christopher Kelvin ; r-help 
> 
> Cc:
> Sent: Thursday, May 31, 2012 3:51 AM
> Subject: Re: [R] problem with ifelse
>
> Since your code has things like this:
> z<-numeric(length(t ((
>
> either you have a serious problem with your email client or you need
> to reread some introductory material and take a hard look at your
> code.
>
> Also note that g() doesn't work, because it contains the statement
> return(m) but m is undefined within g().
>
> Meanwhile, you could provide what I asked: a statement of what you
> expect your code to produce given particular input. Otherwise how
> would we know if we've offered the right solution, since your function
> doesn't work? Using set.seed() would be a useful component of this
> reproducible example.
>
> Without having a working if poorly-written function to go by or a
> clear results statement, I'm not interested in trying to rewrite your
> code. But some thoughts:
>
> Here's a new version of f().
>
> f2 <- function(c1, c2) {
>     r <- pmax(c1 + c2, c1 + 0.5)
>     cbind(c1, r)
> }
>
> It looks like you expected f() to be able to take vectors, but in g()
> you only return one value. Is that a mistake, or what you wanted?
> Since you're also using cbind(), I assume it's a mistake.
>
> Again, there are lots of problems here that suggest that you are
> coming from some other programming language and have not taken the
> time to learn much about R's syntax. This is easily remedied by
> reading the introduction.
>
> Sarah
>
> On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin
>  wrote:
>> Hello Sarah,
>> Thank you for your response. Below is the complete code. My desire is to 
>> obtain interval censored data through simulation to fit it on the weibull 
>> distribution to estimate the parameters. I am actually not very sure of the 
>> code correctness. You may try it and advice me on what to do and also about 
>> it correctness if time will permit you.
>>
>> Thank you
>>
>>
>> g<-function(c1,c2) {
>>   f<-function(c1,c2) {
>>     u<-c1
>>     h<-c1+c2
>>     k<-c1+0.5
>>     r<-numeric (length(c1))
>>     for(i in 1:length(r)) r[i]<-max(h[i],k[i])
>>     return( cbind (u,r))}
>>   r1<-f(c1,c2)
>>   r2<-f(r1[2],r1[1])
>>   r3<-f(r2[2],r2[1])
>>   r4<-f(r3[2],r3[1])
>>   r5<-f(r4[2],r4[1])
>>   a<-(cbind(r1[1],r2[1],r3[1],r4[1],r5[1],r5[2]))
>>   return(m )}
>> c1<-runif(1,0,1.5)
>> c2<-runif(1,0,0.5)
>> m<-g(c1,c2)
>>
>> tdata<-rweibull(25,0.8,1.5)
>> v<-c(0,m,999)
>>
>> y<-function(t,v){
>>   z<-numeric(length(t ((
>>     s<-numeric(length(t ((
>>       for(i in 1:length(t)){
>>         for(j in 1:length(v-1))
>>         { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}}
>>       return(cbind(z,s))}
>>
>> y(t,v)
>>
>>
>>
>>
>> Chris Kelvin
>>
>>
>>
>>
>>
>>
>>
>> Hi,
>>
>> The error with ifelse() seems to be that you have no idea what ifelse() does.
>>
>> As far as I can tell, you tried to construct code that does something like 
>> this:
>>
>>
>> y<-function(tdata,v){
>>    z <- rep(NA, length(tdata))
>>    s <- z
>>    for(i in 1:length(tdata)) {
>>       for(j in 1:length(v-1)) {
>>             if(tdata[i] > v[j] & tdata[i] < v[j+1]) {
>>                 z[i]<-v[j]
>>                 s[i]<-v[j+1]
>>             }
>>         }
>>     }
>>    re

Re: [R] problem with ifelse

2012-05-30 Thread S Ellison


> The code below is used to generate interval censored data but unfortunately 
> there is an
> error with the ifelse which i am not able to rectify.
> Can somebody help correct it for me.

Not quickly.
But cut() might do a large part of what you need with a lot less faffing about; 
see ?cut

S Ellison***
This email and any attachments are confidential. Any use...{{dropped:8}}

__
R-help@r-project.org mailing list
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.


Re: [R] problem with ifelse

2012-05-30 Thread Sarah Goslee
Since your code has things like this:
z<-numeric(length(t ((

either you have a serious problem with your email client or you need
to reread some introductory material and take a hard look at your
code.

Also note that g() doesn't work, because it contains the statement
return(m) but m is undefined within g().

Meanwhile, you could provide what I asked: a statement of what you
expect your code to produce given particular input. Otherwise how
would we know if we've offered the right solution, since your function
doesn't work? Using set.seed() would be a useful component of this
reproducible example.

Without having a working if poorly-written function to go by or a
clear results statement, I'm not interested in trying to rewrite your
code. But some thoughts:

Here's a new version of f().

f2 <- function(c1, c2) {
r <- pmax(c1 + c2, c1 + 0.5)
cbind(c1, r)
}

It looks like you expected f() to be able to take vectors, but in g()
you only return one value. Is that a mistake, or what you wanted?
Since you're also using cbind(), I assume it's a mistake.

Again, there are lots of problems here that suggest that you are
coming from some other programming language and have not taken the
time to learn much about R's syntax. This is easily remedied by
reading the introduction.

Sarah

On Wed, May 30, 2012 at 3:32 PM, Christopher Kelvin
 wrote:
> Hello Sarah,
> Thank you for your response. Below is the complete code. My desire is to 
> obtain interval censored data through simulation to fit it on the weibull 
> distribution to estimate the parameters. I am actually not very sure of the 
> code correctness. You may try it and advice me on what to do and also about 
> it correctness if time will permit you.
>
> Thank you
>
>
> g<-function(c1,c2) {
>   f<-function(c1,c2) {
>     u<-c1
>     h<-c1+c2
>     k<-c1+0.5
>     r<-numeric (length(c1))
>     for(i in 1:length(r)) r[i]<-max(h[i],k[i])
>     return( cbind (u,r))}
>   r1<-f(c1,c2)
>   r2<-f(r1[2],r1[1])
>   r3<-f(r2[2],r2[1])
>   r4<-f(r3[2],r3[1])
>   r5<-f(r4[2],r4[1])
>   a<-(cbind(r1[1],r2[1],r3[1],r4[1],r5[1],r5[2]))
>   return(m )}
> c1<-runif(1,0,1.5)
> c2<-runif(1,0,0.5)
> m<-g(c1,c2)
>
> tdata<-rweibull(25,0.8,1.5)
> v<-c(0,m,999)
>
> y<-function(t,v){
>   z<-numeric(length(t ((
>     s<-numeric(length(t ((
>       for(i in 1:length(t)){
>         for(j in 1:length(v-1))
>         { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}}
>       return(cbind(z,s))}
>
> y(t,v)
>
>
>
>
> Chris Kelvin
>
>
>
>
>
>
>
> Hi,
>
> The error with ifelse() seems to be that you have no idea what ifelse() does.
>
> As far as I can tell, you tried to construct code that does something like 
> this:
>
>
> y<-function(tdata,v){
>    z <- rep(NA, length(tdata))
>    s <- z
>    for(i in 1:length(tdata)) {
>       for(j in 1:length(v-1)) {
>             if(tdata[i] > v[j] & tdata[i] < v[j+1]) {
>                 z[i]<-v[j]
>                 s[i]<-v[j+1]
>             }
>         }
>     }
>    return(cbind(z,s))
> }
>
> But what's with all the (( instead of ))?
>
> And are you certain that the logic in the if statement is correct?
>
> If you tell us what you expect the results to be for given input
> values, we can help with that part too. Including making this more
> Rish: the nested for-loop construct is entirely unnecessary here, but
> I'm disinclined to rewrite it unless I actually know what you're
> trying to achieve.
>
> Incidentally, your example is only nearly-reproducible, since we don't
> know what m is.
>
> Sarah
>
> On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin
>  wrote:
>> Dear all,
>>  The code below is used to generate interval censored data but unfortunately 
>> there is an error with the ifelse which i am not able to rectify.
>>  Can somebody help correct it for me.
>> Thank you
>>
>>
>> t<-rexp(20,0.2)
>> v<-c(0,m,999)
>>
>> y<-function(t,v){
>>   z<-numeric(length(t ((
>>     s<-numeric(length(t ((
>>       for(i in 1:length(t)){
>>         for(j in 1:length(v-1))
>>         { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}}
>>       return(cbind(z,s))}
>>
>> y(t,v)
>>
>
>
-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
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.


Re: [R] problem with ifelse

2012-05-30 Thread Sarah Goslee
Hi,

The error with ifelse() seems to be that you have no idea what ifelse() does.

As far as I can tell, you tried to construct code that does something like this:


y<-function(tdata,v){
   z <- rep(NA, length(tdata))
   s <- z
   for(i in 1:length(tdata)) {
  for(j in 1:length(v-1)) {
if(tdata[i] > v[j] & tdata[i] < v[j+1]) {
z[i]<-v[j]
s[i]<-v[j+1]
}
}
}
   return(cbind(z,s))
}

But what's with all the (( instead of ))?

And are you certain that the logic in the if statement is correct?

If you tell us what you expect the results to be for given input
values, we can help with that part too. Including making this more
Rish: the nested for-loop construct is entirely unnecessary here, but
I'm disinclined to rewrite it unless I actually know what you're
trying to achieve.

Incidentally, your example is only nearly-reproducible, since we don't
know what m is.

Sarah

On Wed, May 30, 2012 at 10:01 AM, Christopher Kelvin
 wrote:
> Dear all,
>  The code below is used to generate interval censored data but unfortunately 
> there is an error with the ifelse which i am not able to rectify.
>  Can somebody help correct it for me.
> Thank you
>
>
> t<-rexp(20,0.2)
> v<-c(0,m,999)
>
> y<-function(t,v){
>   z<-numeric(length(t ((
>     s<-numeric(length(t ((
>       for(i in 1:length(t)){
>         for(j in 1:length(v-1))
>         { ifelse ((t[i]>v[j] & t< v[j+1] ),{z[i]<-v[j];s[i]<-v[j+1]},NA)}}
>       return(cbind(z,s))}
>
> y(t,v)
>


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
R-help@r-project.org mailing list
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.


Re: [R] Problem with ifelse statement

2009-06-23 Thread Daniel Malter
The following works:

socstat=rep(c("A","B","D","E"),each=100)
male=rep(1:4,100)
socstat2=ifelse(socstat=="B"&male==4,"C",socstat)

Therefore, your code should work if it were the code you want (in other
words, it's a logic not a coding problem). Your mistake is that you recode
ALL social statuses of observations that are not "B"  AND MALE!=4 into Bs.
That is, all observations with social status not B (A,D,E,F,G,H,I,J...)  are
recoded into Bs. That is certainly not what you want. I think what you want
is:

data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C",
data$SOCIAL_STATUS)

Right?
Daniel


-
cuncta stricte discussurus
-

-Ursprüngliche Nachricht-
Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im
Auftrag von Mark Na
Gesendet: Tuesday, June 23, 2009 6:33 PM
An: r-help@r-project.org
Betreff: [R] Problem with ifelse statement

Hi R-helpers,

I am trying to use this ifelse statement to recode a variable:
> data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C", 
> "B")

(i.e., if social status is B and there are more than 4 males, then recode
social status to C; otherwise, leave it B)

But, it's not working. See the below R output. Notice that there were
71 "B" observations before the re-code but 2098 "B" observations after the
re-code. The only thing my code should do is REDUCE the number of "B"
observations, not increase them.

Can anyone see what I'm doing wrong? Thanks!

Thanks, Mark Na


> str(data)
'data.frame':   2100 obs. of  13 variables:
 $ DATE   :Class 'Date'  num [1:2100] 14399 14399 14399 14399 14399
...
 $ OBS: Factor w/ 7 levels "AJG","LEB","MB",..: 3 3 3 3 3
3 3 3 3 3 ...
 $ POND_ID: Factor w/ 118 levels "1","10","100",..: 86 86 86
86 86 86 86 86 86 86 ...
 $ STATUS : num  1 1 1 1 1 1 1 1 1 1 ...
 $ SPECIES: Factor w/ 25 levels "AGWT","AMAV",..: 16 16 12 12
4 7 7 7 7 3 ...
 $ SOCIAL_STATUS  : Factor w/ 9 levels "","A","B","D",..: 5 2 5 2 5 5
5 5 2 8 ...
 $ COUNT_OF_GROUPS: num  1 1 1 1 1 3 3 3 1 2 ...
 $ MALE   : num  1 1 1 1 1 1 1 1 1 0 ...
 $ FEMALE : num  1 0 1 0 1 1 1 1 0 0 ...
 $ NOSEX  : num  0 0 0 0 0 0 0 0 0 2 ...
 $ UPLAND : num  0 0 0 0 0 0 0 0 0 0 ...
 $ TAG: num  0 0 0 0 0 0 0 0 0 0 ...
 $ COMMENT: chr  "" "" "" "" ...


> length(which(data$SOCIAL_STATUS=="B"))
[1] 71

> data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C", 
> "B")

> length(which(data$SOCIAL_STATUS=="B"))
[1] 2098

__
R-help@r-project.org mailing list
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.

__
R-help@r-project.org mailing list
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.


Re: [R] Problem with ifelse statement

2009-06-23 Thread jim holtman
Notice what happens in your 'ifelse':  any row that does not meet the
condition is changed to "B"; that is probably not what you were expecting.
You probably want:

data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C",
data$SOCIAL_STATUS)

so you leave the others unchanged.


On Tue, Jun 23, 2009 at 6:32 PM, Mark Na  wrote:

> Hi R-helpers,
>
> I am trying to use this ifelse statement to recode a variable:
> > data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C",
> "B")
>
> (i.e., if social status is B and there are more than 4 males, then
> recode social status to C; otherwise, leave it B)
>
> But, it's not working. See the below R output. Notice that there were
> 71 "B" observations before the re-code but 2098 "B" observations after
> the re-code. The only thing my code should do is REDUCE the number of
> "B" observations, not increase them.
>
> Can anyone see what I'm doing wrong? Thanks!
>
> Thanks, Mark Na
>
>
> > str(data)
> 'data.frame':   2100 obs. of  13 variables:
>  $ DATE   :Class 'Date'  num [1:2100] 14399 14399 14399 14399 14399
> ...
>  $ OBS: Factor w/ 7 levels "AJG","LEB","MB",..: 3 3 3 3 3
> 3 3 3 3 3 ...
>  $ POND_ID: Factor w/ 118 levels "1","10","100",..: 86 86 86
> 86 86 86 86 86 86 86 ...
>  $ STATUS : num  1 1 1 1 1 1 1 1 1 1 ...
>  $ SPECIES: Factor w/ 25 levels "AGWT","AMAV",..: 16 16 12 12
> 4 7 7 7 7 3 ...
>  $ SOCIAL_STATUS  : Factor w/ 9 levels "","A","B","D",..: 5 2 5 2 5 5
> 5 5 2 8 ...
>  $ COUNT_OF_GROUPS: num  1 1 1 1 1 3 3 3 1 2 ...
>  $ MALE   : num  1 1 1 1 1 1 1 1 1 0 ...
>  $ FEMALE : num  1 0 1 0 1 1 1 1 0 0 ...
>  $ NOSEX  : num  0 0 0 0 0 0 0 0 0 2 ...
>  $ UPLAND : num  0 0 0 0 0 0 0 0 0 0 ...
>  $ TAG: num  0 0 0 0 0 0 0 0 0 0 ...
>  $ COMMENT: chr  "" "" "" "" ...
>
>
> > length(which(data$SOCIAL_STATUS=="B"))
> [1] 71
>
> > data$SOCIAL_STATUS<-ifelse(data$SOCIAL_STATUS=="B" & data$MALE>4, "C",
> "B")
>
> > length(which(data$SOCIAL_STATUS=="B"))
> [1] 2098
>
> __
> R-help@r-project.org mailing list
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.