Re: [R] if statement and for loop question

2021-06-01 Thread Michael Dewey

Dear Kai

When you ask again it is best to tell us what your input is and what 
output you were hoping for and what you actually got. If you can make a 
small data-set which shows all that then your post will be much more 
likely to get a helpful response. If you want to transfer the data-set 
to us then using dput() will ensure that we have exactly the same as you 
have. Do not forget to tell us what libraries, if any, you have loaded too.


Michael

On 31/05/2021 17:26, Kai Yang via R-help wrote:

   Hi Jim,
Sorry to post "same" question, because
1. I was asking to use plain text format. I have to post my question again. But 
I don't know if it is working.
2. I'm a beginner for R (< 2 month). It may not easy for me to ask a "clear" R 
question. My current work is to transfer my SAS code into R, especially for data 
manipulation part.
I'll do my best to ask a non."same" question later.
Thanks,
Kai
 On Sunday, May 30, 2021, 10:44:41 PM PDT, Jim Lemon  
wrote:
  
  Hi Kai,

You seem to be asking the same question again and again. This does not
give us the warm feeling that you know what you want.

testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"),
  b=c("No","Yes","No","Maybe","Yes"),
  c=c("Off","On","Off","Off","On"),
  d=c("Bad","Good","Bad","Bad","Good"),
  stringsAsFactors=FALSE)
testdf
match_strings<-c("Positive","VUS")
testdf$b<-ifelse(testdf$a %in% match_strings,testdf$b,"")
testdf$c<-ifelse(testdf$a %in% match_strings,testdf$c,"")
testdf$d<-ifelse(testdf$a %in% match_strings,testdf$d,"")
testdf

I have assumed that you mean "zero length strings" rather than
"zeros". Also note that your initial code was producing logical values
that were never assigned to anything.

Jim

On Mon, May 31, 2021 at 2:29 AM Kai Yang via R-help
 wrote:


Hello List,I have a data frame which having the character columns:

| a1 | b1 | c1 | d1 |
| a2 | b2 | c2 | d2 |
| a3 | b3 | c3 | d3 |
| a4 | b4 | c4 | d4 |
| a5 | b5 | c5 | d5 |



I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 and 
d1 will be zero out. And do the same thing for the a2 to a5 series.
I write the code below to do this. But it doesn't work. Would you please 
correct my code?
Thank you,
Kai


for (i in 1:5)
{
   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
   {
     try$b[i]== ''
     try$c[i] == ''
     try$d[i]== ''
   }
}


         [[alternative HTML version deleted]]

__
R-help@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]]


__
R-help@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.



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
R-help@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.


Re: [R] if statement and for loop question

2021-05-31 Thread Kai Yang via R-help
  Hi Jim,
Sorry to post "same" question, because 
1. I was asking to use plain text format. I have to post my question again. But 
I don't know if it is working.
2. I'm a beginner for R (< 2 month). It may not easy for me to ask a "clear" R 
question. My current work is to transfer my SAS code into R, especially for 
data manipulation part. 
I'll do my best to ask a non."same" question later.
Thanks,
Kai
On Sunday, May 30, 2021, 10:44:41 PM PDT, Jim Lemon  
wrote:  
 
 Hi Kai,
You seem to be asking the same question again and again. This does not
give us the warm feeling that you know what you want.

testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"),
 b=c("No","Yes","No","Maybe","Yes"),
 c=c("Off","On","Off","Off","On"),
 d=c("Bad","Good","Bad","Bad","Good"),
 stringsAsFactors=FALSE)
testdf
match_strings<-c("Positive","VUS")
testdf$b<-ifelse(testdf$a %in% match_strings,testdf$b,"")
testdf$c<-ifelse(testdf$a %in% match_strings,testdf$c,"")
testdf$d<-ifelse(testdf$a %in% match_strings,testdf$d,"")
testdf

I have assumed that you mean "zero length strings" rather than
"zeros". Also note that your initial code was producing logical values
that were never assigned to anything.

Jim

On Mon, May 31, 2021 at 2:29 AM Kai Yang via R-help
 wrote:
>
> Hello List,I have a data frame which having the character columns:
>
> | a1 | b1 | c1 | d1 |
> | a2 | b2 | c2 | d2 |
> | a3 | b3 | c3 | d3 |
> | a4 | b4 | c4 | d4 |
> | a5 | b5 | c5 | d5 |
>
>
>
> I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 
> and d1 will be zero out. And do the same thing for the a2 to a5 series.
> I write the code below to do this. But it doesn't work. Would you please 
> correct my code?
> Thank you,
> Kai
>
>
> for (i in 1:5)
> {
>  if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>  {
>    try$b[i]== ''
>    try$c[i] == ''
>    try$d[i]== ''
>  }
> }
>
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@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]]

__
R-help@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.


Re: [R] if statement and for loop question

2021-05-30 Thread Jim Lemon
Hi Kai,
You seem to be asking the same question again and again. This does not
give us the warm feeling that you know what you want.

testdf<-data.frame(a=c("Negative","Positive","Neutral","Random","VUS"),
 b=c("No","Yes","No","Maybe","Yes"),
 c=c("Off","On","Off","Off","On"),
 d=c("Bad","Good","Bad","Bad","Good"),
 stringsAsFactors=FALSE)
testdf
match_strings<-c("Positive","VUS")
testdf$b<-ifelse(testdf$a %in% match_strings,testdf$b,"")
testdf$c<-ifelse(testdf$a %in% match_strings,testdf$c,"")
testdf$d<-ifelse(testdf$a %in% match_strings,testdf$d,"")
testdf

I have assumed that you mean "zero length strings" rather than
"zeros". Also note that your initial code was producing logical values
that were never assigned to anything.

Jim

On Mon, May 31, 2021 at 2:29 AM Kai Yang via R-help
 wrote:
>
> Hello List,I have a data frame which having the character columns:
>
> | a1 | b1 | c1 | d1 |
> | a2 | b2 | c2 | d2 |
> | a3 | b3 | c3 | d3 |
> | a4 | b4 | c4 | d4 |
> | a5 | b5 | c5 | d5 |
>
>
>
> I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 
> and d1 will be zero out. And do the same thing for the a2 to a5 series.
> I write the code below to do this. But it doesn't work. Would you please 
> correct my code?
> Thank you,
> Kai
>
>
> for (i in 1:5)
> {
>   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>   {
> try$b[i]== ''
> try$c[i] == ''
> try$d[i]== ''
>   }
> }
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


Re: [R] if statement and for loop question

2021-05-30 Thread Rui Barradas

Hello,

You don't need a loop, the R way is a vectorized solution and it's also 
clearer.

Create a logical index (note only one &) and assign b, c, d where it's TRUE.


i <- try$a != "Positive" & try$a != "VUS"
try <- within(try, {
  b[i] <- ''
  c[i] <- ''
  d[i] <- ''
})


Hope this helps,

Rui Barradas

Às 17:28 de 30/05/21, Kai Yang via R-help escreveu:

Hello List,I have a data frame which having the character columns:

| a1 | b1 | c1 | d1 |
| a2 | b2 | c2 | d2 |
| a3 | b3 | c3 | d3 |
| a4 | b4 | c4 | d4 |
| a5 | b5 | c5 | d5 |



I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 and 
d1 will be zero out. And do the same thing for the a2 to a5 series.
I write the code below to do this. But it doesn't work. Would you please 
correct my code?
Thank you,
Kai


for (i in 1:5)
{
   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
   {
     try$b[i]== ''
     try$c[i] == ''
     try$d[i]== ''
   }
}


[[alternative HTML version deleted]]

__
R-help@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.



__
R-help@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.


Re: [R] if statement and for loop question

2021-05-30 Thread Berry, Charles
Kai,

You have made a simple mistake. And now you cannot see it.  I believe this is 
not uncommon among programmers.  It has happened to me more times than I want 
to recall.

> On May 30, 2021, at 9:28 AM, Kai Yang via R-help  wrote:
> 
> Hello List,I have a data frame which having the character columns:
> 
> | a1 | b1 | c1 | d1 |
> | a2 | b2 | c2 | d2 |
> | a3 | b3 | c3 | d3 |
> | a4 | b4 | c4 | d4 |
> | a5 | b5 | c5 | d5 |
> 
> 
> 
> I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 
> and d1 will be zero out. And do the same thing for the a2 to a5 series.
> I write the code below to do this. But it doesn't work. Would you please 
> correct my code?
> Thank you,
> Kai
> 
> 
> for (i in 1:5) 
> {
>   if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>   {
> try$b[i]== ''

What is the above line intended to do?

I think you need to read the line out loud stating the effect of each operator.

If you still do not see what gives, copy and paste each line to the R prompt 
after the loop has run (i.e. with `i' having value 5) paying attention to any 
results that are printed out.

> try$c[i] == ''
> try$d[i]== ''
>   }
> }
> 
> 
>   [[alternative HTML version deleted]]
> 

HTH,

Chuck

p.s. It is highly recommended to use `<-' as the assignment operator.

__
R-help@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.


Re: [R] if statement and for loop question

2021-05-30 Thread Jeff Newmiller
Can you make R code that creates an actual sample data frame that looks like 
you want the answer to look? say, just using the data.frame function and 
literal strings. Oh, and read the Posting Guide... you need to send your email 
using plain text format or it may get garbled when the list strips the HTML 
formatting.

On May 30, 2021 9:28:52 AM PDT, Kai Yang via R-help  
wrote:
>Hello List,I have a data frame which having the character columns:
>
>| a1 | b1 | c1 | d1 |
>| a2 | b2 | c2 | d2 |
>| a3 | b3 | c3 | d3 |
>| a4 | b4 | c4 | d4 |
>| a5 | b5 | c5 | d5 |
>
>
>
>I need to do: if a1 not = "Positive" and not = "VUS" then values of 
>b1, c1 and d1 will be zero out. And do the same thing for the a2 to a5
>series.
>I write the code below to do this. But it doesn't work. Would you
>please correct my code?
>Thank you,
>Kai
>
>
>for (i in 1:5) 
>{
>  if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
>  {
>    try$b[i]== ''
>    try$c[i] == ''
>    try$d[i]== ''
>  }
>}
>
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


[R] if statement and for loop question

2021-05-30 Thread Kai Yang via R-help
Hello List,I have a data frame which having the character columns:

| a1 | b1 | c1 | d1 |
| a2 | b2 | c2 | d2 |
| a3 | b3 | c3 | d3 |
| a4 | b4 | c4 | d4 |
| a5 | b5 | c5 | d5 |



I need to do: if a1 not = "Positive" and not = "VUS" then values of  b1, c1 and 
d1 will be zero out. And do the same thing for the a2 to a5 series.
I write the code below to do this. But it doesn't work. Would you please 
correct my code?
Thank you,
Kai


for (i in 1:5) 
{
  if (isTRUE(try$a[i] != "Positive" && try$a[i] != "VUS"))
  {
    try$b[i]== ''
    try$c[i] == ''
    try$d[i]== ''
  }
}


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] If statement

2019-09-12 Thread Jeff Newmiller
Use ifelse function, not if. If is only good for one logical value at a time, 
but you are working with long vectors of values simultaneously. I have no 
interest in doing all of your workfor you, but the concept is

cpl_or_sngl <- dat1$b %in% c( "couple", "single" )
a_pvt <- "private" == dat1$a
dat1$z <- with( dat1, ifelse( a_pvt & cpl_or_sngl, a, "Zero" ) )
dat1$y <- with( dat1, ifelse( a_pvt & cpl_or_sngl, c, 0 ) )

On September 12, 2019 4:06:59 PM PDT, Val  wrote:
>Hi all,
>
>I am trying to use the  if else statement and create  two new columns
>based on the existing two columns.  Below please find my sample data,
>
>dat1 <-read.table(text="ID  a b c d
>A private couple  25 35
>B private single  24 38
>C none  single28 32
>E none none 20 36 ",header=TRUE,stringsAsFactors=F)
>
>dat1$z <- "Zero"
>dat1$y <-  0
>
>if a is "private" and (b is either "couple" rr "single"
>then  z value = a's value   and y value = c's value
>if a is "none" and  ( b is either couple of single then  z= private
>  then  z value =b's value  qnd  y value= d's value
>else z value= Zero and y value=0
>
>the desired out put looks like
>ID  a  b  c d z   y
>1  A private couple 25 35 private 25
>2  B private single 24 38 private 24
>3  Cnone single 28 32 single  32
>4  Enone   none 20 36 Zero0
>
>my attempt
>
>if (dat1$a =="private"  &  (dat1$b =="couple"| dat1$b =="single"))
>{
>  dat1$z  <-   dat1$a
>  dat1$y  <-   dat1$c
>}
>
>else if (dat1$a =="none"  &  (dat1$b =="couple"| dat1$b =="single")) {
>dat1$z  <-   dat1$b
>dat1$y  <-   dat1$c
>  }
>else
>{ default value}
>did not wok, how could I fix this?
>Thank you in advance
>
>__
>R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


Re: [R] If statement

2019-09-12 Thread Bert Gunter
You appear to be confusing && with &  and || with |  ; (the first of each
pair take a logical expression, the second of each a logical vector) ...
as well as if ... else with ifelse (the first is a flow control statement
taking a logical expression; the second is a function taking a logical
vector as an argument).  I suggest you spend some time with an appropriate
R tutorial to clarify your understanding. You'll get a better explanation
and examples if you do so than anything I can provide you.

Incidentally, in future, do not tell us "it did not work." Provide the
specific error message that burped out.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Sep 12, 2019 at 4:07 PM Val  wrote:

> Hi all,
>
> I am trying to use the  if else statement and create  two new columns
> based on the existing two columns.  Below please find my sample data,
>
> dat1 <-read.table(text="ID  a b c d
> A private couple  25 35
> B private single  24 38
> C none  single28 32
> E none none 20 36 ",header=TRUE,stringsAsFactors=F)
>
> dat1$z <- "Zero"
> dat1$y <-  0
>
> if a is "private" and (b is either "couple" rr "single"
> then  z value = a's value   and y value = c's value
> if a is "none" and  ( b is either couple of single then  z= private
>   then  z value =b's value  qnd  y value= d's value
> else z value= Zero and y value=0
>
> the desired out put looks like
> ID  a  b  c d z   y
> 1  A private couple 25 35 private 25
> 2  B private single 24 38 private 24
> 3  Cnone single 28 32 single  32
> 4  Enone   none 20 36 Zero0
>
> my attempt
>
> if (dat1$a =="private"  &  (dat1$b =="couple"| dat1$b =="single"))
> {
>   dat1$z  <-   dat1$a
>   dat1$y  <-   dat1$c
> }
>
> else if (dat1$a =="none"  &  (dat1$b =="couple"| dat1$b =="single")) {
> dat1$z  <-   dat1$b
> dat1$y  <-   dat1$c
>   }
> else
> { default value}
> did not wok, how could I fix this?
> Thank you in advance
>
> __
> R-help@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]]

__
R-help@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.


[R] If statement

2019-09-12 Thread Val
Hi all,

I am trying to use the  if else statement and create  two new columns
based on the existing two columns.  Below please find my sample data,

dat1 <-read.table(text="ID  a b c d
A private couple  25 35
B private single  24 38
C none  single28 32
E none none 20 36 ",header=TRUE,stringsAsFactors=F)

dat1$z <- "Zero"
dat1$y <-  0

if a is "private" and (b is either "couple" rr "single"
then  z value = a's value   and y value = c's value
if a is "none" and  ( b is either couple of single then  z= private
  then  z value =b's value  qnd  y value= d's value
else z value= Zero and y value=0

the desired out put looks like
ID  a  b  c d z   y
1  A private couple 25 35 private 25
2  B private single 24 38 private 24
3  Cnone single 28 32 single  32
4  Enone   none 20 36 Zero0

my attempt

if (dat1$a =="private"  &  (dat1$b =="couple"| dat1$b =="single"))
{
  dat1$z  <-   dat1$a
  dat1$y  <-   dat1$c
}

else if (dat1$a =="none"  &  (dat1$b =="couple"| dat1$b =="single")) {
dat1$z  <-   dat1$b
dat1$y  <-   dat1$c
  }
else
{ default value}
did not wok, how could I fix this?
Thank you in advance

__
R-help@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.


Re: [R] Case statement in sqldf

2017-09-12 Thread Mangalani Peter Makananisa
Thank you very much,

I will work on it

-Original Message-
From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] 
Sent: 11 September 2017 06:50 PM
To: Mangalani Peter Makananisa
Cc: r-help@r-project.org
Subject: Re: Case statement in sqldf

2018-03-3 in your code should be 2018-03-31.

The line
then'201415'
needs to be fixed.

When posting please provide minimal self-contained examples. There was no input 
provided and library statements not relevant to the posted code were included.

Fixing the invalid date and bad line, getting rid of those library statements 
that are unnecessary and providing some test input, it works for me for the 
input shown.

(Note that it would NOT work if we omitted library(RH2) since the default 
sqlite back end does not have date types and does not know that an R date -- 
which is sent to sqlite as the number of days since
1970-01-01 -- corresponds to a particular character string; however, the H2 
database does have date types.  See FAQ #4 on the sqldf github home page for 
more info.
https://github.com/ggrothendieck/sqldf
)

This works:

library(sqldf)
library(RH2)

cr <- data.frame(ReportDate = as.Date("2017-09-11")) # input

cr2 =  sqldf(" select ReportDate
 ,  case
   when ReportDate between  '2012-04-01'  and  '2013-03-31'
   then '2012_13'
   when  ReportDate between '2013-04-01'  and  '2014-03-31'
   then '2013_14'
   when  ReportDate between  '2014-04-01'  and  '2015-03-31'
   then '2014_15'
   when ReportDate between '2015-04-01'  and  '2016-03-31'
   then '2015_16'
   when ReportDate between '2016-04-01'  and  '2017-03-31'
   then '2016_17'
   when ReportDate between '2017-04-01'  and  '2018-03-31'
  then '2017_18' else null
 end as FY
 from cr
 where  ReportDate  >=  '2012-04-01'
 ")

giving:

  > cr2
ReportDate  FY
  1 2017-09-11 2017_18

Note that using as.yearqtr from zoo this alternative could be used:

library(zoo)
cr <- data.frame(ReportDate = as.Date("2017-09-11")) # input

fy <- as.integer(as.yearqtr(cr$ReportDate) + 3/4) transform(cr, FY = 
paste0(fy-1, "_", fy %% 100))

giving:

  ReportDate  FY
1 2017-09-11 2017_18


On Mon, Sep 11, 2017 at 4:05 AM, Mangalani Peter Makananisa 
 wrote:
> Hi all,
>
>
>
> I am trying to create a new  variable called Fiscal Year (FY) using 
> case expression in sqldf  and I am getting a null FY , see the code below .
>
>
>> +then '2017_18' else null>> South African 
>> + Revenue Service (SARS)>> Specialist: Statistical Support>> TCEI_OR 
>> + (Head Office)>> Tell: +272 422 7357, Cell: +2782 456 4669>> 
>> + http://www.sars.gov.za/Pages/Email-disclaimer.aspxemail: 
>> + ggrothendieck at gmail.with
> Please advise me as to how I can do this mutation.
>
>
>
>   library(zoo)
>
>   library(lubridate)
>
>   library(stringr)
>
>   library(RH2)
>
>   library(sqldf)
>
>
>
> cr$ReportDate = as.Date(cr$ReportDate, format ='%Y-%m-%d')
>
>
>
>> cr2 =  sqldf(" select ReportDate
>
> +  ,  case
>
> +when ReportDate between  '2012-04-01'  and
> '2013-03-31'
>
> +then '2012_13'
>
> +when  ReportDate between '2013-04-01'  and
> '2014-03-31'
>
> +then '2013_14'
>
> +when  ReportDate between  '2014-04-01'  and
> '2015-03-31'
>
> +then'201415'
>
> +when ReportDate between '2015-04-01'  and
> '2016-03-31'
>
> +then '2015_16'
>
> +when ReportDate between '2016-04-01'  and
> '2017-03-31'
>
> +then '2016_17'
>
> +when ReportDate between '2017-04-01'  and
> '2018-03-3'
>


> +end as FY
>
> +   from cr
>
> +  where  ReportDate  >=  '2012-04-01'
>
> +  ")
>
>
>
> Thanking you in advance
>
>
>
> Kind regards,
>
>
>
> Mangalani Peter Makananisa (0005786)
>








>
>
>
>
> Disclaimer
>
> Please Note: This email and its contents are subject to our email 
> legal notice which can be viewed at




--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP

Please Note: This email and its contents are subject to our email legal notice 
which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx

[[alternative HTML version deleted]]

__
R-help@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, 

Re: [R] Case statement in sqldf

2017-09-12 Thread Mangalani Peter Makananisa
Thanks D,

I will work on the solution you gave and give feedback.

-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: 11 September 2017 05:19 PM
To: Mangalani Peter Makananisa
Cc: r-help@r-project.org
Subject: Re: [R] Case statement in sqldf


> On Sep 11, 2017, at 1:05 AM, Mangalani Peter Makananisa 
> <pmakanan...@sars.gov.za> wrote:
> 
> Hi all,
> 
> I am trying to create a new  variable called Fiscal Year (FY) using case 
> expression in sqldf  and I am getting a null FY , see the code below .
> 
> Please advise me as to how I can do this mutation.
> 
>  library(zoo)
>  library(lubridate)
>  library(stringr)
>  library(RH2)
>  library(sqldf)
> 
> cr$ReportDate = as.Date(cr$ReportDate, format ='%Y-%m-%d')
> 
>> cr2 =  sqldf(" select ReportDate
> +  ,  case
> +when ReportDate between  '2012-04-01'  and  
> '2013-03-31'
> +then '2012_13'
> +when  ReportDate between '2013-04-01'  and  
> '2014-03-31'
> +then '2013_14'
> +when  ReportDate between  '2014-04-01'  and  
> '2015-03-31'
> +then'201415'
> +when ReportDate between '2015-04-01'  and  
> '2016-03-31'
> +then '2015_16'
> +when ReportDate between '2016-04-01'  and  
> '2017-03-31'
> +then '2016_17'
> +when ReportDate between '2017-04-01'  and  
> '2018-03-3'
> +then '2017_18' else null
> +end as FY
> +   from cr
> +  where  ReportDate  >=  '2012-04-01'
> +  ")

There was no cr object in any of the package I loaded although `lubridate` and 
`stringr` appear unnecessary and were omitted. I get no error with your code 
using this test object:

 cr <- data.frame(ReportDate = seq(as.Date("1970-01-01"), 
as.Date("2020-01-01"), by="1 year" ))

> cr2 =  sqldf(" select ReportDate
+  ,  case
+when ReportDate between  '2012-04-01'  and  
'2013-03-31'
+then '2012_13'
+when  ReportDate between '2013-04-01'  and  
'2014-03-31'
+then '2013_14'
+when  ReportDate between  '2014-04-01'  and  
'2015-03-31'
+then'201415'
+when ReportDate between '2015-04-01'  and  '2016-03-31'
+then '2015_16'
+when ReportDate between '2016-04-01'  and  '2017-03-31'
+then '2016_17'
+when ReportDate between '2017-04-01'  and  '2018-03-3'
+then '2017_18' else null
+end as FY
+   from cr
+  where  ReportDate  >=  '2012-04-01'
+  ")
> 
> str(cr2)
'data.frame':   8 obs. of  2 variables:
 $ ReportDate: Date, format: "2013-01-01" "2014-01-01" "2015-01-01" ...
 $ FY: chr  "2012_13" "2013_14" "201415" "2015_16" ...
> 
> 
> Thanking you in advance
> 
> Kind regards,
> 
> Mangalani Peter Makananisa (0005786)
> South African Revenue Service (SARS)
> Specialist: Statistical Support
> TCEI_OR (Head Office)
> Tell: +272 422 7357, Cell: +2782 456 4669
> 
> Please Note: This email and its contents are subject to our email 
> legal notice which can be viewed at 
> http://www.sars.gov.za/Pages/Email-disclaimer.aspx
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   
-Gehm's Corollary to Clarke's Third Law

Please Note: This email and its contents are subject to our email legal notice 
which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Case statement in sqldf

2017-09-11 Thread Gabor Grothendieck
2018-03-3 in your code should be 2018-03-31.

The line
then'201415'
needs to be fixed.

When posting please provide minimal self-contained examples. There was
no input provided and library statements not relevant to the posted
code were included.

Fixing the invalid date and bad line, getting rid of those library
statements that are unnecessary and providing some test input, it
works for me for the input shown.

(Note that it would NOT work if we omitted library(RH2) since the
default sqlite back end does not have date types and does not know
that an R date -- which is sent to sqlite as the number of days since
1970-01-01 -- corresponds to a particular character string; however,
the H2 database does have date types.  See FAQ #4 on the sqldf github
home page for more info.
https://github.com/ggrothendieck/sqldf
)

This works:

library(sqldf)
library(RH2)

cr <- data.frame(ReportDate = as.Date("2017-09-11")) # input

cr2 =  sqldf(" select ReportDate
 ,  case
   when ReportDate between  '2012-04-01'  and  '2013-03-31'
   then '2012_13'
   when  ReportDate between '2013-04-01'  and  '2014-03-31'
   then '2013_14'
   when  ReportDate between  '2014-04-01'  and  '2015-03-31'
   then '2014_15'
   when ReportDate between '2015-04-01'  and  '2016-03-31'
   then '2015_16'
   when ReportDate between '2016-04-01'  and  '2017-03-31'
   then '2016_17'
   when ReportDate between '2017-04-01'  and  '2018-03-31'
  then '2017_18' else null
 end as FY
 from cr
 where  ReportDate  >=  '2012-04-01'
 ")

giving:

  > cr2
ReportDate  FY
  1 2017-09-11 2017_18

Note that using as.yearqtr from zoo this alternative could be used:

library(zoo)
cr <- data.frame(ReportDate = as.Date("2017-09-11")) # input

fy <- as.integer(as.yearqtr(cr$ReportDate) + 3/4)
transform(cr, FY = paste0(fy-1, "_", fy %% 100))

giving:

  ReportDate  FY
1 2017-09-11 2017_18


On Mon, Sep 11, 2017 at 4:05 AM, Mangalani Peter Makananisa
 wrote:
> Hi all,
>
>
>
> I am trying to create a new  variable called Fiscal Year (FY) using case
> expression in sqldf  and I am getting a null FY , see the code below .
>
>
>> +then '2017_18' else null>> South African Revenue 
>> Service (SARS)>> Specialist: Statistical Support>> TCEI_OR (Head Office)>> 
>> Tell: +272 422 7357, Cell: +2782 456 4669>> 
>> http://www.sars.gov.za/Pages/Email-disclaimer.aspxemail: ggrothendieck at 
>> gmail.with
> Please advise me as to how I can do this mutation.
>
>
>
>   library(zoo)
>
>   library(lubridate)
>
>   library(stringr)
>
>   library(RH2)
>
>   library(sqldf)
>
>
>
> cr$ReportDate = as.Date(cr$ReportDate, format ='%Y-%m-%d')
>
>
>
>> cr2 =  sqldf(" select ReportDate
>
> +  ,  case
>
> +when ReportDate between  '2012-04-01'  and
> '2013-03-31'
>
> +then '2012_13'
>
> +when  ReportDate between '2013-04-01'  and
> '2014-03-31'
>
> +then '2013_14'
>
> +when  ReportDate between  '2014-04-01'  and
> '2015-03-31'
>
> +then'201415'
>
> +when ReportDate between '2015-04-01'  and
> '2016-03-31'
>
> +then '2015_16'
>
> +when ReportDate between '2016-04-01'  and
> '2017-03-31'
>
> +then '2016_17'
>
> +when ReportDate between '2017-04-01'  and
> '2018-03-3'
>


> +end as FY
>
> +   from cr
>
> +  where  ReportDate  >=  '2012-04-01'
>
> +  ")
>
>
>
> Thanking you in advance
>
>
>
> Kind regards,
>
>
>
> Mangalani Peter Makananisa (0005786)
>








>
>
>
>
> Disclaimer
>
> Please Note: This email and its contents are subject to our email legal
> notice which can be viewed at




-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP

__
R-help@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.


Re: [R] Case statement in sqldf

2017-09-11 Thread David Winsemius

> On Sep 11, 2017, at 1:05 AM, Mangalani Peter Makananisa 
>  wrote:
> 
> Hi all,
> 
> I am trying to create a new  variable called Fiscal Year (FY) using case 
> expression in sqldf  and I am getting a null FY , see the code below .
> 
> Please advise me as to how I can do this mutation.
> 
>  library(zoo)
>  library(lubridate)
>  library(stringr)
>  library(RH2)
>  library(sqldf)
> 
> cr$ReportDate = as.Date(cr$ReportDate, format ='%Y-%m-%d')
> 
>> cr2 =  sqldf(" select ReportDate
> +  ,  case
> +when ReportDate between  '2012-04-01'  and  
> '2013-03-31'
> +then '2012_13'
> +when  ReportDate between '2013-04-01'  and  
> '2014-03-31'
> +then '2013_14'
> +when  ReportDate between  '2014-04-01'  and  
> '2015-03-31'
> +then'201415'
> +when ReportDate between '2015-04-01'  and  
> '2016-03-31'
> +then '2015_16'
> +when ReportDate between '2016-04-01'  and  
> '2017-03-31'
> +then '2016_17'
> +when ReportDate between '2017-04-01'  and  
> '2018-03-3'
> +then '2017_18' else null
> +end as FY
> +   from cr
> +  where  ReportDate  >=  '2012-04-01'
> +  ")

There was no cr object in any of the package I loaded although `lubridate` and 
`stringr` appear unnecessary and were omitted. I get no error with your code 
using this test object:

 cr <- data.frame(ReportDate = seq(as.Date("1970-01-01"), 
as.Date("2020-01-01"), by="1 year" ))

> cr2 =  sqldf(" select ReportDate
+  ,  case
+when ReportDate between  '2012-04-01'  and  
'2013-03-31'
+then '2012_13'
+when  ReportDate between '2013-04-01'  and  
'2014-03-31'
+then '2013_14'
+when  ReportDate between  '2014-04-01'  and  
'2015-03-31'
+then'201415'
+when ReportDate between '2015-04-01'  and  '2016-03-31'
+then '2015_16'
+when ReportDate between '2016-04-01'  and  '2017-03-31'
+then '2016_17'
+when ReportDate between '2017-04-01'  and  '2018-03-3'
+then '2017_18' else null
+end as FY
+   from cr
+  where  ReportDate  >=  '2012-04-01'
+  ")
> 
> str(cr2)
'data.frame':   8 obs. of  2 variables:
 $ ReportDate: Date, format: "2013-01-01" "2014-01-01" "2015-01-01" ...
 $ FY: chr  "2012_13" "2013_14" "201415" "2015_16" ...
> 
> 
> Thanking you in advance
> 
> Kind regards,
> 
> Mangalani Peter Makananisa (0005786)
> South African Revenue Service (SARS)
> Specialist: Statistical Support
> TCEI_OR (Head Office)
> Tell: +272 422 7357, Cell: +2782 456 4669
> 
> Please Note: This email and its contents are subject to our email legal 
> notice which can be viewed at 
> http://www.sars.gov.za/Pages/Email-disclaimer.aspx
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   
-Gehm's Corollary to Clarke's Third Law

__
R-help@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.


[R] Case statement in sqldf

2017-09-11 Thread Mangalani Peter Makananisa
Hi all,

I am trying to create a new  variable called Fiscal Year (FY) using case 
expression in sqldf  and I am getting a null FY , see the code below .

Please advise me as to how I can do this mutation.

  library(zoo)
  library(lubridate)
  library(stringr)
  library(RH2)
  library(sqldf)

cr$ReportDate = as.Date(cr$ReportDate, format ='%Y-%m-%d')

> cr2 =  sqldf(" select ReportDate
+  ,  case
+when ReportDate between  '2012-04-01'  and  
'2013-03-31'
+then '2012_13'
+when  ReportDate between '2013-04-01'  and  
'2014-03-31'
+then '2013_14'
+when  ReportDate between  '2014-04-01'  and  
'2015-03-31'
+then'201415'
+when ReportDate between '2015-04-01'  and  '2016-03-31'
+then '2015_16'
+when ReportDate between '2016-04-01'  and  '2017-03-31'
+then '2016_17'
+when ReportDate between '2017-04-01'  and  '2018-03-3'
+then '2017_18' else null
+end as FY
+   from cr
+  where  ReportDate  >=  '2012-04-01'
+  ")

Thanking you in advance

Kind regards,

Mangalani Peter Makananisa (0005786)
South African Revenue Service (SARS)
Specialist: Statistical Support
TCEI_OR (Head Office)
Tell: +272 422 7357, Cell: +2782 456 4669

Please Note: This email and its contents are subject to our email legal notice 
which can be viewed at http://www.sars.gov.za/Pages/Email-disclaimer.aspx

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] R [loop statement ]

2016-04-16 Thread Jim Lemon
Hi tan sj,
It is by no means easy to figure out what you want without the code,
but If I read your message correctly, you can run the loops either
way. When you have nested loops producing output, it is often a good
idea to include the parameters for each run in the output as well as
the result so that you don't get the results mixed up:

cat(N,mean,...,p.value,"\n")

Jim


On Sat, Apr 16, 2016 at 12:06 PM, tan sj  wrote:
> hi, i am new in this field.
>
> I am now writing a code in robustness simulation study. I have written a 
> brief code "for loop" for the factor (samples sizes d,std deviation ) , i 
> wish to test them in gamma distribution with equal and unequal skewness, with 
> the above for loop in a single code if possible. Can i ask is that any 
> suitable loop statement for this situation.
>
> This is my ideas ,all the two for loop runs then run for the first set of 
> gamma distribution with equal skewness then store the result,
> then,
> all the two for loops run again and then run for the second set of gamma 
> distribution with unequal skewness then store the result again.
>
> if-else statement look like is not quite suitable in this situation as it 
> will run one set only.
> Please, i hope can get some help here. Thanks.
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


[R] R [loop statement ]

2016-04-15 Thread tan sj
hi, i am new in this field.

I am now writing a code in robustness simulation study. I have written a brief 
code "for loop" for the factor (samples sizes d,std deviation ) , i wish to 
test them in gamma distribution with equal and unequal skewness, with the above 
for loop in a single code if possible. Can i ask is that any suitable loop 
statement for this situation.

This is my ideas ,all the two for loop runs then run for the first set of gamma 
distribution with equal skewness then store the result,
then,
all the two for loops run again and then run for the second set of gamma 
distribution with unequal skewness then store the result again.

if-else statement look like is not quite suitable in this situation as it will 
run one set only.
Please, i hope can get some help here. Thanks.

[[alternative HTML version deleted]]

__
R-help@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.


[R] 'which' statement for recode?

2015-01-22 Thread Charles Stangor
Hi,

Is there a way to easily convert the list of course terms into sequential
integers in the dataframe (see code below)?

eg. 199801 = 1; 199808=2

I know I can use recode but shouldn't which work?

Thanks in advance!


sc = data.frame(c(200208, 200701, 201201))
names(sc) = c(TERM)

TermList = c(NA, 199801, 199808, 199901, 199908, 21,
28, 200101, 200108,
 200201, 200208, 200301, 200308, 200401, 200408,
200501, 200508,
 200601, 200608, 200701, 200708, 200801, 200808,
200901, 200908,
 201001, 201008, 201101, 201108, 201201, 201308,
201401, 201408)

which(TermList==sc$TERM)

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] 'which' statement for recode?

2015-01-22 Thread Sarah Goslee
I'm not quite sure, but I think you might want:
 which(TermList %in% sc$TERM)
[1] 11 20 30

instead. Using == ends up with automatic recycling and other things you
probably weren't expecting.

Sarah

On Thu, Jan 22, 2015 at 11:01 AM, Charles Stangor cstan...@gmail.com
wrote:
 Hi,

 Is there a way to easily convert the list of course terms into sequential
 integers in the dataframe (see code below)?

 eg. 199801 = 1; 199808=2

 I know I can use recode but shouldn't which work?

 Thanks in advance!


 sc = data.frame(c(200208, 200701, 201201))
 names(sc) = c(TERM)

 TermList = c(NA, 199801, 199808, 199901, 199908, 21,
 28, 200101, 200108,
  200201, 200208, 200301, 200308, 200401, 200408,
 200501, 200508,
  200601, 200608, 200701, 200708, 200801, 200808,
 200901, 200908,
  201001, 201008, 201101, 201108, 201201, 201308,
 201401, 201408)

 which(TermList==sc$TERM)



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

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] 'which' statement for recode?

2015-01-22 Thread William Dunlap
You did not show what answer you expected, but does the following do what
you want?

   match(sc$TERM, TermList)
  [1] 11 20 30

Making a factor whose levels are TermList may also be useful.  (The
exclude=NULL
is to factor doesn't drop NA from the levels).
   sc$fTERM - factor(sc$fTERM, levels=TermList, exclude=NULL)
   str(sc)
  'data.frame':   3 obs. of  2 variables:
   $ TERM : Factor w/ 3 levels 200208,200701,..: 1 2 3
   $ fTERM: Factor w/ 33 levels NA,199801,199808,..: 11 20 30



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Jan 22, 2015 at 8:01 AM, Charles Stangor cstan...@gmail.com wrote:

 Hi,

 Is there a way to easily convert the list of course terms into sequential
 integers in the dataframe (see code below)?

 eg. 199801 = 1; 199808=2

 I know I can use recode but shouldn't which work?

 Thanks in advance!


 sc = data.frame(c(200208, 200701, 201201))
 names(sc) = c(TERM)

 TermList = c(NA, 199801, 199808, 199901, 199908, 21,
 28, 200101, 200108,
  200201, 200208, 200301, 200308, 200401, 200408,
 200501, 200508,
  200601, 200608, 200701, 200708, 200801, 200808,
 200901, 200908,
  201001, 201008, 201101, 201108, 201201, 201308,
 201401, 201408)

 which(TermList==sc$TERM)

 [[alternative HTML version deleted]]

 __
 R-help@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]]

__
R-help@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.


Re: [R] 'which' statement for recode?

2015-01-22 Thread John Fox
Dear Charles,

When you put TERM in a data frame, by default it was converted to a factor,
which is already stored as integers. Thus, if I understand correctly what
you want, the following will work:

 sc$TERM
[1] 200208 200701 201201
Levels: 200208 200701 201201

 unclass(sc$TERM)
[1] 1 2 3
attr(,levels)
[1] 200208 200701 201201

 class(unclass(sc$TERM))
[1] integer

Note that the original class designations are presented in the levels
attribute.

This assumes that all of the class designations that you want to use appear
in sc$TERM. You already received some suggestions for the way that you
originally posed the problem (where there were classes in TermList not
appearing in sc$TERM).

I hope this helps,
 John

---
John Fox, Professor
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/



 -Original Message-
 From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Charles
 Stangor
 Sent: January-22-15 11:01 AM
 To: r-help@r-project.org
 Subject: [R] 'which' statement for recode?
 
 Hi,
 
 Is there a way to easily convert the list of course terms into sequential
 integers in the dataframe (see code below)?
 
 eg. 199801 = 1; 199808=2
 
 I know I can use recode but shouldn't which work?
 
 Thanks in advance!
 
 
 sc = data.frame(c(200208, 200701, 201201))
 names(sc) = c(TERM)
 
 TermList = c(NA, 199801, 199808, 199901, 199908, 21,
28,
 200101, 200108,
  200201, 200208, 200301, 200308, 200401, 200408,
200501,
 200508,
  200601, 200608, 200701, 200708, 200801, 200808,
200901,
 200908,
  201001, 201008, 201101, 201108, 201201, 201308,
201401,
 201408)
 
 which(TermList==sc$TERM)
 
   [[alternative HTML version deleted]]
 
 __
 R-help@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.


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

__
R-help@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.


Re: [R] 'which' statement for recode?

2015-01-22 Thread Jeff Newmiller
?factor

You might find the description in the Introduction to R that comes with the R 
software helpful if the help page seems terse.

Please read the Posting Guide and particular post using plain text format, a 
strong in your email program.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On January 22, 2015 8:01:25 AM PST, Charles Stangor cstan...@gmail.com wrote:
Hi,

Is there a way to easily convert the list of course terms into
sequential
integers in the dataframe (see code below)?

eg. 199801 = 1; 199808=2

I know I can use recode but shouldn't which work?

Thanks in advance!


sc = data.frame(c(200208, 200701, 201201))
names(sc) = c(TERM)

TermList = c(NA, 199801, 199808, 199901, 199908, 21,
28, 200101, 200108,
200201, 200208, 200301, 200308, 200401, 200408,
200501, 200508,
200601, 200608, 200701, 200708, 200801, 200808,
200901, 200908,
201001, 201008, 201101, 201108, 201201, 201308,
201401, 201408)

which(TermList==sc$TERM)

   [[alternative HTML version deleted]]

__
R-help@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.

__
R-help@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.


[R] If statement not working in a for loop when making it independent

2014-09-29 Thread Nebulo Archae
Dear all,

I have a data.frame xy that contains numeric data and data_qual which contains 
qualitative data which I want to include in a for loop with an if statement 
(commented out in the code below).
The if statement should be applied if the ID in data_qual$ID is the same than 
in xy$ID.

I am trying to make it independent by doing this:

if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){...}
 
but it doesn't work when I incorporate it in the for loop. The logical result 
is correct but it only works for the first element when implemented in the for 
loop. Can anyone see what I am doing wrong here?

###
Here is my code:
xy 
-data.frame(NAME=c(NAME1,NAME1,NAME1,NAME2,NAME2,NAME2,NAME3,NAME3),ID=c(87,87,87,199,199,199,233,233),X_START_YEAR=c(1950,1988,1994,1899,1909,1924,1945,1948),Y_START_VALUE=c(75,25,-90,-8,-55,-10,-9,12),X_END_YEAR=c(1985,1994,1999,1904,1924,1987,1946,1949),
 Y_END_VALUE=c(20,50,-15,-70,-80,-100,24,59))
 
data_qual - 
data.frame(NAME=c(NAME2,NAME3),ID=c(199,233),X_START_YEAR=c(1986,1905), 
Y_START_VALUE=c(-X,ST),X_END_YEAR=c(1987,1907),Y_END_VALUE=c(-X,ST))
 
# split xy by group as defined by ID
ind - split(xy,xy$ID)
 
for (i in ind){xx = unlist(i[,grep('X_',colnames(i))]) 
               yy = unlist(i[,grep('Y_',colnames(i))]) 
               fname - paste0(i[1, 'ID'], '.png') 
               png(fname, width=1679, height=1165, res=150)
  if(any(xx  1946)) {my_x_lim - c(min(xx), 2014)} else {my_x_lim - c(1946, 
2014)} 
  par(mar=c(6,8,6,5))
  plot(xx, yy,main=unique(i[,1]),xlab=Time [Years],ylab=Value 
[m],pch=21,xlim = my_x_lim,font.lab=2, cex.lab=1.2, cex.axis=1.1)
  i - i[,-1]
  segments(i[,2],i[,3],i[,4],i[,5],lwd=2)
 
 # if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){
 #     rel_data_qual - data_qual[data_qual$ID %in% i$ID,]
 #     text(x = rel_data_qual$X_END_YEAR,
 #          y = min(i$Y_END_VALUE + 3),
 #     labels = rel_data_qual$Y_END_VALUE)  
 #   }
 
  dev.off()
}

Thanks, Kurt

__
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] If statement not working in a for loop when making it independent

2014-09-29 Thread Jeff Newmiller
You have really tied yourself up in a knot here. Last I checked, when A==B, 
then B==A. You also need to study the difference between ?if and ?ifelse, since 
you are not giving the if function the scalar it expects. For example, i$ID is 
a vector of three (identical, due to your use of split) values, so %in% will 
return three logical values.
I think one way to do this is to replace

i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID

with

i[ 1, ID ] %in% data_qual[ , ID]

---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On September 29, 2014 4:05:54 PM PDT, Nebulo Archae nebulos...@gmx.ch wrote:
Dear all,

I have a data.frame xy that contains numeric data and data_qual which
contains qualitative data which I want to include in a for loop with an
if statement (commented out in the code below).
The if statement should be applied if the ID in data_qual$ID is the
same than in xy$ID.

I am trying to make it independent by doing this:

if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){...}
 
but it doesn't work when I incorporate it in the for loop. The logical
result is correct but it only works for the first element when
implemented in the for loop. Can anyone see what I am doing wrong here?

###
Here is my code:
xy
-data.frame(NAME=c(NAME1,NAME1,NAME1,NAME2,NAME2,NAME2,NAME3,NAME3),ID=c(87,87,87,199,199,199,233,233),X_START_YEAR=c(1950,1988,1994,1899,1909,1924,1945,1948),Y_START_VALUE=c(75,25,-90,-8,-55,-10,-9,12),X_END_YEAR=c(1985,1994,1999,1904,1924,1987,1946,1949),
Y_END_VALUE=c(20,50,-15,-70,-80,-100,24,59))
 
data_qual -
data.frame(NAME=c(NAME2,NAME3),ID=c(199,233),X_START_YEAR=c(1986,1905),
Y_START_VALUE=c(-X,ST),X_END_YEAR=c(1987,1907),Y_END_VALUE=c(-X,ST))
 
# split xy by group as defined by ID
ind - split(xy,xy$ID)
 
for (i in ind){xx = unlist(i[,grep('X_',colnames(i))]) 
               yy = unlist(i[,grep('Y_',colnames(i))]) 
               fname - paste0(i[1, 'ID'], '.png') 
               png(fname, width=1679, height=1165, res=150)
  if(any(xx  1946)) {my_x_lim - c(min(xx), 2014)} else {my_x_lim -
c(1946, 2014)} 
  par(mar=c(6,8,6,5))
  plot(xx, yy,main=unique(i[,1]),xlab=Time [Years],ylab=Value
[m],pch=21,xlim = my_x_lim,font.lab=2, cex.lab=1.2, cex.axis=1.1)
  i - i[,-1]
  segments(i[,2],i[,3],i[,4],i[,5],lwd=2)
 
 # if(i$ID %in% data_qual[data_qual$ID %in% i$ID,]$ID){
 #     rel_data_qual - data_qual[data_qual$ID %in% i$ID,]
 #     text(x = rel_data_qual$X_END_YEAR,
 #          y = min(i$Y_END_VALUE + 3),
 #     labels = rel_data_qual$Y_END_VALUE)  
 #   }
 
  dev.off()
}

Thanks, Kurt

__
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] Ifelse statement on a factor level data frame

2014-09-28 Thread Patrick Burns

I believe you are in Circle 8.2.7 of
The R Inferno.

http://www.burns-stat.com/documents/books/the-r-inferno/

Pat

On 28/09/2014 05:49, Kate Ignatius wrote:

Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

__
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.



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
 'Impatient R'
 'The R Inferno'
 'Tao Te Programming')

__
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] Ifelse statement on a factor level data frame

2014-09-28 Thread Kate Ignatius
Strange that,

I did put everything with as.character but all I got was the same...

class of dbpmn[,2]) = factor
class of dbpmn[,21]  = factor
class of  dbpmn[,20] = data.frame

This has to be a problem ???

I can put reproducible output here but not sure if this going to of
help here. I think its all about factors and data frames and
characters...

K.

On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon j...@bitwrit.com.au wrote:
 On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
 Quick question:

 I am running the following code on some variables that are factors:

 dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
 as.character(dbpmn[,(21)]), dbpmn[,20], '')

 Instead of returning some value it gives me this:

 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

 Playing around with the code, gives me some kind of variation to it.
 Is there some way to get me what I want.  The variable that its
 suppose to give back is a bunch of sampleIDs.

 Hi Kate,
 If I create a little example:

 dbpmn-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
   V2=factor(sample(LETTERS[1:4],20,TRUE)),
   V3=factor(sample(LETTERS[1:4],20,TRUE)))
 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  dbpmn[,3],)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  1
 7   D  D  D  4
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  1
 14  D  C  A
 15  C  D  B
 16  A  A  B  2
 17  A  C  C
 18  B  B  C  3
 19  C  C  C  3
 20  D  D  D  4

 I get what I expect, the numeric value of the third element in dbpmn
 where the first two elements are equal. I think what you want is:

 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  as.character(dbpmn[,3]),)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  A
 7   D  D  D  D
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  A
 14  D  C  A
 15  C  D  B
 16  A  A  B  B
 17  A  C  C
 18  B  B  C  C
 19  C  C  C  C
 20  D  D  D  D

 Jim


__
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] Ifelse statement on a factor level data frame

2014-09-28 Thread Bert Gunter
Inline.

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom.
Clifford Stoll




On Sun, Sep 28, 2014 at 6:38 AM, Kate Ignatius kate.ignat...@gmail.com wrote:
 Strange that,

 I did put everything with as.character but all I got was the same...

 class of dbpmn[,2]) = factor
 class of dbpmn[,21]  = factor
 class of  dbpmn[,20] = data.frame

 This has to be a problem ???

Indeed -- your failure to read documentation.

I suggest you do your due diligence, read Pat Burns's link, and follow
the advice given you by posting a reproducible example. More than
likely the last will be unnecessary as you will figure it out in the
course of doing what you should do.

Cheers,
Bert


 I can put reproducible output here but not sure if this going to of
 help here. I think its all about factors and data frames and
 characters...

 K.

 On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon j...@bitwrit.com.au wrote:
 On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
 Quick question:

 I am running the following code on some variables that are factors:

 dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
 as.character(dbpmn[,(21)]), dbpmn[,20], '')

 Instead of returning some value it gives me this:

 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

 Playing around with the code, gives me some kind of variation to it.
 Is there some way to get me what I want.  The variable that its
 suppose to give back is a bunch of sampleIDs.

 Hi Kate,
 If I create a little example:

 dbpmn-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
   V2=factor(sample(LETTERS[1:4],20,TRUE)),
   V3=factor(sample(LETTERS[1:4],20,TRUE)))
 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  dbpmn[,3],)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  1
 7   D  D  D  4
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  1
 14  D  C  A
 15  C  D  B
 16  A  A  B  2
 17  A  C  C
 18  B  B  C  3
 19  C  C  C  3
 20  D  D  D  4

 I get what I expect, the numeric value of the third element in dbpmn
 where the first two elements are equal. I think what you want is:

 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  as.character(dbpmn[,3]),)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  A
 7   D  D  D  D
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  A
 14  D  C  A
 15  C  D  B
 16  A  A  B  B
 17  A  C  C
 18  B  B  C  C
 19  C  C  C  C
 20  D  D  D  D

 Jim


 __
 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] Ifelse statement on a factor level data frame

2014-09-28 Thread Kate Ignatius
Apologies - you're right.  Missed it in the pdf.

K.

On Sun, Sep 28, 2014 at 10:22 AM, Bert Gunter gunter.ber...@gene.com wrote:
 Inline.

 Bert Gunter
 Genentech Nonclinical Biostatistics
 (650) 467-7374

 Data is not information. Information is not knowledge. And knowledge
 is certainly not wisdom.
 Clifford Stoll




 On Sun, Sep 28, 2014 at 6:38 AM, Kate Ignatius kate.ignat...@gmail.com 
 wrote:
 Strange that,

 I did put everything with as.character but all I got was the same...

 class of dbpmn[,2]) = factor
 class of dbpmn[,21]  = factor
 class of  dbpmn[,20] = data.frame

 This has to be a problem ???

 Indeed -- your failure to read documentation.

 I suggest you do your due diligence, read Pat Burns's link, and follow
 the advice given you by posting a reproducible example. More than
 likely the last will be unnecessary as you will figure it out in the
 course of doing what you should do.

 Cheers,
 Bert


 I can put reproducible output here but not sure if this going to of
 help here. I think its all about factors and data frames and
 characters...

 K.

 On Sun, Sep 28, 2014 at 1:15 AM, Jim Lemon j...@bitwrit.com.au wrote:
 On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
 Quick question:

 I am running the following code on some variables that are factors:

 dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
 as.character(dbpmn[,(21)]), dbpmn[,20], '')

 Instead of returning some value it gives me this:

 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

 Playing around with the code, gives me some kind of variation to it.
 Is there some way to get me what I want.  The variable that its
 suppose to give back is a bunch of sampleIDs.

 Hi Kate,
 If I create a little example:

 dbpmn-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
   V2=factor(sample(LETTERS[1:4],20,TRUE)),
   V3=factor(sample(LETTERS[1:4],20,TRUE)))
 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  dbpmn[,3],)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  1
 7   D  D  D  4
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  1
 14  D  C  A
 15  C  D  B
 16  A  A  B  2
 17  A  C  C
 18  B  B  C  3
 19  C  C  C  3
 20  D  D  D  4

 I get what I expect, the numeric value of the third element in dbpmn
 where the first two elements are equal. I think what you want is:

 dbpmn[4]-
  ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
  as.character(dbpmn[,3]),)
 dbpmn
V1 V2 V3 V4
 1   B  D  C
 2   C  A  D
 3   C  B  A
 4   A  B  C
 5   B  D  B
 6   D  D  A  A
 7   D  D  D  D
 8   B  C  A
 9   B  D  B
 10  D  C  A
 11  A  D  C
 12  A  C  B
 13  A  A  A  A
 14  D  C  A
 15  C  D  B
 16  A  A  B  B
 17  A  C  C
 18  B  B  C  C
 19  C  C  C  C
 20  D  D  D  D

 Jim


 __
 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] Ifelse statement on a factor level data frame

2014-09-28 Thread William Dunlap
ifelse() often has problems constructing the right type of return value.

if you want to keep the data as a factor (with its existing levels)
use x[condition] - value instead of ifelse(condition, value, x).  E.g.,
x - factor(c(Large,Small,Small,XLarge),
levels=c(Small,Med,Large,XLarge))
x
   [1] Large  Small  Small  XLarge
   Levels: Small Med Large XLarge
XLarge2Large - function(x) { x[x==XLarge] - Large ; x }
XLarge2Large(x)
   [1] Large Small Small Large
   Levels: Small Med Large XLarge
instead of things like
ifelse(x==XLarge, Large, x)
   [1] 3 1 1 Large

If you don't care about the factor levels, then convert x to a character vector.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Sep 27, 2014 at 10:13 PM, Jeff Newmiller
jdnew...@dcn.davis.ca.us wrote:
 Not reproducible, ball in your court. However, in the meantime, my suggestion 
 is to not do that. Convert to character before you alter the factor, then 
 convert back when you are done.
 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
   Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
 /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.

 On September 27, 2014 9:49:41 PM PDT, Kate Ignatius kate.ignat...@gmail.com 
 wrote:
Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

__
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.

__
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] Ifelse statement on a factor level data frame

2014-09-27 Thread Kate Ignatius
Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

__
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] Ifelse statement on a factor level data frame

2014-09-27 Thread Jeff Newmiller
Not reproducible, ball in your court. However, in the meantime, my suggestion 
is to not do that. Convert to character before you alter the factor, then 
convert back when you are done.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

On September 27, 2014 9:49:41 PM PDT, Kate Ignatius kate.ignat...@gmail.com 
wrote:
Quick question:

I am running the following code on some variables that are factors:

dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
as.character(dbpmn[,(21)]), dbpmn[,20], '')

Instead of returning some value it gives me this:

c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))

Playing around with the code, gives me some kind of variation to it.
Is there some way to get me what I want.  The variable that its
suppose to give back is a bunch of sampleIDs.

Thanks!

__
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] Ifelse statement on a factor level data frame

2014-09-27 Thread Jim Lemon
On Sun, 28 Sep 2014 12:49:41 AM Kate Ignatius wrote:
 Quick question:
 
 I am running the following code on some variables that are factors:
 
 dbpmn$IID1new - ifelse(as.character(dbpmn[,2]) ==
 as.character(dbpmn[,(21)]), dbpmn[,20], '')
 
 Instead of returning some value it gives me this:
 
 c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
 
 Playing around with the code, gives me some kind of variation to it.
 Is there some way to get me what I want.  The variable that its
 suppose to give back is a bunch of sampleIDs.
 
Hi Kate,
If I create a little example:

dbpmn-data.frame(V1=factor(sample(LETTERS[1:4],20,TRUE)),
  V2=factor(sample(LETTERS[1:4],20,TRUE)),
  V3=factor(sample(LETTERS[1:4],20,TRUE)))
dbpmn[4]-
 ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
 dbpmn[,3],)
dbpmn
   V1 V2 V3 V4
1   B  D  C   
2   C  A  D   
3   C  B  A   
4   A  B  C   
5   B  D  B   
6   D  D  A  1
7   D  D  D  4
8   B  C  A   
9   B  D  B   
10  D  C  A   
11  A  D  C   
12  A  C  B   
13  A  A  A  1
14  D  C  A   
15  C  D  B   
16  A  A  B  2
17  A  C  C   
18  B  B  C  3
19  C  C  C  3
20  D  D  D  4

I get what I expect, the numeric value of the third element in dbpmn 
where the first two elements are equal. I think what you want is:

dbpmn[4]-
 ifelse(as.character(dbpmn[,1]) == as.character(dbpmn[,(2)]),
 as.character(dbpmn[,3]),)
dbpmn
   V1 V2 V3 V4
1   B  D  C   
2   C  A  D   
3   C  B  A   
4   A  B  C   
5   B  D  B   
6   D  D  A  A
7   D  D  D  D
8   B  C  A   
9   B  D  B   
10  D  C  A   
11  A  D  C   
12  A  C  B   
13  A  A  A  A
14  D  C  A   
15  C  D  B   
16  A  A  B  B
17  A  C  C   
18  B  B  C  C
19  C  C  C  C
20  D  D  D  D

Jim

__
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] ifelse statement with two vectors of different length

2013-12-18 Thread arun
Hi,
Please show a reproducible example.

countrydiff - c(Albania, Algeria, Belarus, Canada, Germany)
long_df - data.frame(country_name = c(Algeria, Guyana, Hungary, 
Algeria, Canada, Iran, Iran, Norway,Uruguay, Zimbabwe) )
 ifelse(long_df$country_name %in% countrydiff,1,0)
# [1] 1 0 0 1 1 0 0 0 0 0
#or
1*(long_df$country_name %in% countrydiff)
# [1] 1 0 0 1 1 0 0 0 0 0
A.K.




Dear list-members, 

I have the following problem: I have a vector (countrydiff) with
 length 72 and another vector (long_df$country_name) which is about 
12000 long. Basically what I want to do is to if the factor level (or 
string name) in long_df$country_name appears on the countrydiff, then 
long_df$povdat should be equal to 1, if it does not appear on the 
countrydiff vector then long_df$povdat should be equal to zero. I have 
tried different combinations and read some. The following code should in
 my mind do it, but it doesn’t: 

long_df$povdat-ifelse(long_df$country_name == countrydiff, 1, 0) 

long_df$povdat-ifelse(long_df$country_name %in% countrydiff, 1, 0) 

Additional information: the factor vector countrydiff contains 
unique country names (Albania, Zimbabwe etc.), whereas 
long_df$country_name also contains country names albeit not unique since
 it is in longform. The unique names that appear in long_df$country_name
 is around 200. 


Any suggestions? 
Thanks in advance. 

Best 
Adel

__
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] ifelse statement with two vectors of different length

2013-12-18 Thread Adel

Dear list-members,

I have the following problem: I have a vector (countrydiff) with length 72
and another vector (long_df$country_name) which is about 12000 long.
Basically what I want to do is to if the factor level (or string name) in
long_df$country_name appears on the countrydiff, then long_df$povdat should
be equal to 1, if it does not appear on the countrydiff vector then
long_df$povdat should be equal to zero. I have tried different combinations
and read some. The following code should in my mind do it, but it doesn’t:

long_df$povdat-ifelse(long_df$country_name == countrydiff, 1, 0)

long_df$povdat-ifelse(long_df$country_name %in% countrydiff, 1, 0)

Additional information: the factor vector countrydiff contains unique
country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
contains country names albeit not unique since it is in longform. The unique
names that appear in long_df$country_name is around 200.


Any suggestions?
Thanks in advance.

Best
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401.html
Sent from the R help mailing list archive at Nabble.com.

__
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] ifelse statement with two vectors of different length

2013-12-18 Thread Adel

Dear Arun

Thanks for your reply, it made me realize that the problem was not in the
code but in the levels() of the factors. Some countries had some extra
spacing which made the ifelse() function not work. So if I modify your code
(added space to countrydiff), it will then look something like this:

countrydiff - c(Albania, Algeria, Belarus, Canada   ,
Germany   ) 
long_df - data.frame(country_name = c(Algeria, Guyana, Hungary,
Algeria, Canada, Iran, Iran, Norway,Uruguay, Zimbabwe) ) 

I had to use the gsub to fix this first.


Interestingly, the setdiff() function did not react on spacing difference
which I used before coming to the ifelse statement and therefore I did not
react on this in the first place

#no reaction from R on spacing diff.
setdiff(countrydiff, long_df$country_name)


Nevertheless, thanks again for being helpful!
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401p4682403.html
Sent from the R help mailing list archive at Nabble.com.

__
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] ifelse statement with two vectors of different length

2013-12-18 Thread arun
Hi Adel,

If the problem is the spacing, then
library(stringr)
1*(long_df$country_name %in% str_trim(countrydiff))
# [1] 1 0 0 1 1 0 0 0 0 0
A.K.


Dear Arun 

Thanks for your reply, it made me realize that the problem was 
not in the code but in the levels() of the factors. Some countries had 
some extra spacing which made the ifelse() function not work. So if I 
modify your code (added space to countrydiff), it will then look 
something like this: 

countrydiff - c(Albania    , Algeria    , Belarus    , Canada   , 
Germany   ) 
long_df - data.frame(country_name = c(Algeria, Guyana, 
Hungary, Algeria, Canada, Iran, Iran, Norway,Uruguay, 
Zimbabwe) ) 

I had to use the gsub to fix this first. 


Interestingly, the setdiff() function did not react on 
spacing difference which I used before coming to the ifelse statement 
and therefore I did not react on this in the first place 

#no reaction from R on spacing diff. 
setdiff(countrydiff, long_df$country_name) 


Nevertheless, thanks again for being helpful! 
Adel 


On Wednesday, December 18, 2013 9:58 AM, Adel adel.da...@sociology.gu.se 
wrote:

Dear list-members,

I have the following problem: I have a vector (countrydiff) with length 72
and another vector (long_df$country_name) which is about 12000 long.
Basically what I want to do is to if the factor level (or string name) in
long_df$country_name appears on the countrydiff, then long_df$povdat should
be equal to 1, if it does not appear on the countrydiff vector then
long_df$povdat should be equal to zero. I have tried different combinations
and read some. The following code should in my mind do it, but it doesn’t:

long_df$povdat-ifelse(long_df$country_name == countrydiff, 1, 0)

long_df$povdat-ifelse(long_df$country_name %in% countrydiff, 1, 0)

Additional information: the factor vector countrydiff contains unique
country names (Albania, Zimbabwe etc.), whereas long_df$country_name also
contains country names albeit not unique since it is in longform. The unique
names that appear in long_df$country_name is around 200.


Any suggestions?
Thanks in advance.

Best
Adel




--
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-with-two-vectors-of-different-length-tp4682401.html
Sent from the R help mailing list archive at Nabble.com.

__
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] If-statement in for-loop

2013-12-11 Thread Gerrit Eichner

Hello, gncl dzgn,

your problem has a flavor of homework which is usually not delt with on 
this list. However, a few comments:


0. The description of your problem is rather vague, in particular, the 
meaning of input in the description of your conditions is unclear! (By 
the way, your main problem is probably not the inclusion of the 
conditions; see 2.)


Note: PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html and provide commented, 
minimal, self-contained, reproducible code.

^^^

1. Do not use variable names like t, T, c, s because they already exist as 
R-objects.


2. Have you noticed the warnings In s = d : longer object length is not 
a multiple of shorter object length and In s + b * (0:T) : longer object 
length is not a multiple of shorter object length? Apparently the 
involved objects (s, d, b and T) do not fit together. Hint: Compare the 
lenght of s with the lengths of d and 0:T!


3. Do not run the code all at once, but either without the for-loops line 
by line with j and i set to approriate values (like i = 1 and j = 2), or 
with the for-loops, but with N and T very small, e. g. N = 1 and T = 2 to 
start with. Alternatively, you could also take a look at the function 
browser() (and include it in the bodies of your for-loops).


4. Another hint: Your if-else-statement

  if(x  minx) {
s[i] - minx
   } else {
if(x  maxx) {
  s[i] - maxx
 } else s[i] - x
   }

appears be simplifiable to

  s[i] - min( max( x, minx), maxx)


 Hth  --  Gerrit



Hi everyone,

you might find my question elementary but I am a beginner and 
unfortunately I can't fix the problem.


So, I simulate this following algorithm and some values of c are NA. 
Therefore, I should add these following two if-statements but I don't 
know how I should do it in a for-loop.


Thank in advance if someone helps me!

The conditions:

If there is no input greater or equal to d, then ALG = b*T
If  max(s +  b*(0:T))  b*T , then OPT = b*T


The algorithm:

a - 0.0008
b - 0.0001
T - 100
t - 0:T
alpha - 1

d- sqrt(a * b) * T - b * t

N - 100
c - rep(0, N)
for (j in 1:N) {

e - rnorm(T, mean = 0, sd = 0.001)
s- c( runif(1,0, a*T), rep(0, T-1) )
minx - 0
for(i in 2:T) {
   x - alpha * s[i-1] + e[i]
maxx - a*(T-i)
if(x  minx) {
s[i] - minx
} else {
if(x  maxx) {
s[i] - maxx
} else s[i] - x
}
}

p- which(s = d)[1]
ALG- s[p] + b*(p-1)
OPT - max(s +  b*(0:T))

c[j] -  OPT / ALG

}

head(c)
mean(c)
plot(c)



__
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] If-statement in for-loop

2013-12-10 Thread gncl dzgn
Hi everyone,

you might find my question elementary but I am a beginner and unfortunately I 
can't fix the problem. 

So, I simulate this following algorithm and some values of c are NA. Therefore, 
I should add these following two if-statements but I don't know how I should do 
it in a for-loop. 

Thank in advance if someone helps me!

The conditions: 

If there is no input greater or equal to d, then ALG = b*T
If  max(s +  b*(0:T))  b*T , then OPT = b*T


The algorithm:

a - 0.0008
b - 0.0001 
T - 100 
t - 0:T 
alpha - 1 

d- sqrt(a * b) * T - b * t

N - 100
c - rep(0, N)
for (j in 1:N) {

e - rnorm(T, mean = 0, sd = 0.001) 
s- c( runif(1,0, a*T), rep(0, T-1) ) 
minx - 0 
for(i in 2:T) { 
x - alpha * s[i-1] + e[i] 
maxx - a*(T-i) 
if(x  minx) { 
 s[i] - minx 
} else { 
if(x  maxx) { 
 s[i] - maxx 
} else s[i] - x 
} 
} 

p- which(s = d)[1]
ALG- s[p] + b*(p-1)
OPT - max(s +  b*(0:T))

c[j] -  OPT / ALG

}

head(c)
mean(c)
plot(c)



  
[[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.


[R] Two statement logical dealing with NAs

2012-12-10 Thread Hans Thompson
Hello.

I have a two statement logical that if NA is returned for the second
statement I want to rely on result of the first statement.  I still would
like to use both when I can though.

x - c(1:5)
y - c(1,2,NA,4,5)
x  5  x-y == 0

How can I trick R to refer back to (x  5) where it is NA on the third
value?

Thanks.

[[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.


Re: [R] Two statement logical dealing with NAs

2012-12-10 Thread Duncan Murdoch

On 12-12-10 7:55 PM, Hans Thompson wrote:

Hello.

I have a two statement logical that if NA is returned for the second
statement I want to rely on result of the first statement.  I still would
like to use both when I can though.

x - c(1:5)
y - c(1,2,NA,4,5)
x  5  x-y == 0

How can I trick R to refer back to (x  5) where it is NA on the third
value?


Use is.na() to test for NA.  I think this does what you want:

x  5  (is.na(x-y == 0) | x-y == 0)

Duncan Murdoch

__
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] Two statement logical dealing with NAs

2012-12-10 Thread David Winsemius


On Dec 10, 2012, at 4:55 PM, Hans Thompson wrote:


Hello.

I have a two statement logical that if NA is returned for the second
statement I want to rely on result of the first statement.  I still  
would

like to use both when I can though.

x - c(1:5)
y - c(1,2,NA,4,5)
x  5  x-y == 0

How can I trick R to refer back to (x  5) where it is NA on the third
value?


If you program so that you trick your tool, you will fail to  
progress in understanding it


Since NA | TRUE returns TRUE  (and is clearly documented as such) this  
is not trickery:


 x  5  (is.na(y) | x-y == 0)
[1]  TRUE  TRUE  TRUE  TRUE FALSE

--
David Winsemius, MD
Alameda, CA, USA

__
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] If statement - copying a factor variable to a new variable

2012-06-29 Thread peter dalgaard

On Jun 28, 2012, at 09:42 , Rui Barradas wrote:

 Hello,
 
 Another way is to use index vectors:
 
 
 v1.factor - c(S,S,D,D,D,NA)
 v2.factor - c(D,D,S,S,S,S)
 
 td2 - test.data - data.frame(v1.factor,v2.factor)
 
 for (i in 1:nrow(test.data) ) {
 
[... etc ...]
 
 } #End FOR
 
 # Create index vectors
 na1 - is.na(v1.factor)
 na2 - is.na(v2.factor)
 
 # Create 'newvar' with default value
 td2$newvar - NA
 # Now, set values if condition is met.
 td2$newvar[!na1  !na2] - as.character(td2$v1.factor[!na1 !na2])
 
 all.equal(test.data, td2)
 [1] TRUE
 

Shouldn't this rather be something like

new - td2$v1.factor
i - is.na(new)
new[i] - td2$v2.factor[i]
td2$newvar - new

?? 
(Caveat: things could go wrong if the levels differ.)

 
 I find this way better when doing a multiple if/else based on combinations of 
 a small number of conditions. It's also very readable.
 
 Hope this helps,
 
 Rui Barradas
 
 Em 28-06-2012 08:00, Miguel Manese escreveu:
 Hi James,
 
 On Thu, Jun 28, 2012 at 12:33 AM, James Holland holland.ag...@gmail.com 
 wrote:
 I need to look through a dataset with two factor variables, and depending
 on certain criteria, create a new variable containing the data from one of
 those other variables.
 
 The problem is, R keeps making my new variable an integer and saving the
 data as a 1 or 2 (I believe the levels of the factor).
 
 I've tried using as.factor in the IF output statement, but that doesn't
 seem to work.
 
 Any help is appreciated.
 
 
 
 #Sample code
 
 rm(list=ls())
 
 
 v1.factor - c(S,S,D,D,D,NA)
 v2.factor - c(D,D,S,S,S,S)
 
 test.data - data.frame(v1.factor,v2.factor)
 
 The vectorized way to do that would be
 
 # v1.factor if present, else v2.factor
 test.data$newvar - ifelse(!is.na(v1.factor), v1.factor, v2.factor)
 
 I suggest you work with the character levels first then convert it
 into a factor, e.g. if v1.factor  v2.factor are already factors, do:
 
 test.data$newvar - as.factor(ifelse(!is.na(v1.factor),
 as.character(v1.factor), as.character(v2.factor)))
 
 
 
 Regards,
 
 Jon
 
 __
 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] If statement - copying a factor variable to a new variable

2012-06-28 Thread Miguel Manese
Hi James,

On Thu, Jun 28, 2012 at 12:33 AM, James Holland holland.ag...@gmail.com wrote:
 I need to look through a dataset with two factor variables, and depending
 on certain criteria, create a new variable containing the data from one of
 those other variables.

 The problem is, R keeps making my new variable an integer and saving the
 data as a 1 or 2 (I believe the levels of the factor).

 I've tried using as.factor in the IF output statement, but that doesn't
 seem to work.

 Any help is appreciated.



 #Sample code

 rm(list=ls())


 v1.factor - c(S,S,D,D,D,NA)
 v2.factor - c(D,D,S,S,S,S)

 test.data - data.frame(v1.factor,v2.factor)

The vectorized way to do that would be

# v1.factor if present, else v2.factor
test.data$newvar - ifelse(!is.na(v1.factor), v1.factor, v2.factor)

I suggest you work with the character levels first then convert it
into a factor, e.g. if v1.factor  v2.factor are already factors, do:

test.data$newvar - as.factor(ifelse(!is.na(v1.factor),
as.character(v1.factor), as.character(v2.factor)))



Regards,

Jon

__
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] If statement - copying a factor variable to a new variable

2012-06-28 Thread Rui Barradas

Hello,

Another way is to use index vectors:


v1.factor - c(S,S,D,D,D,NA)
v2.factor - c(D,D,S,S,S,S)

td2 - test.data - data.frame(v1.factor,v2.factor)

for (i in 1:nrow(test.data) ) {

[... etc ...]

} #End FOR

# Create index vectors
na1 - is.na(v1.factor)
na2 - is.na(v2.factor)

# Create 'newvar' with default value
td2$newvar - NA
# Now, set values if condition is met.
td2$newvar[!na1  !na2] - as.character(td2$v1.factor[!na1 !na2])

all.equal(test.data, td2)
[1] TRUE


I find this way better when doing a multiple if/else based on 
combinations of a small number of conditions. It's also very readable.


Hope this helps,

Rui Barradas

Em 28-06-2012 08:00, Miguel Manese escreveu:

Hi James,

On Thu, Jun 28, 2012 at 12:33 AM, James Holland holland.ag...@gmail.com wrote:

I need to look through a dataset with two factor variables, and depending
on certain criteria, create a new variable containing the data from one of
those other variables.

The problem is, R keeps making my new variable an integer and saving the
data as a 1 or 2 (I believe the levels of the factor).

I've tried using as.factor in the IF output statement, but that doesn't
seem to work.

Any help is appreciated.



#Sample code

rm(list=ls())


v1.factor - c(S,S,D,D,D,NA)
v2.factor - c(D,D,S,S,S,S)

test.data - data.frame(v1.factor,v2.factor)


The vectorized way to do that would be

# v1.factor if present, else v2.factor
test.data$newvar - ifelse(!is.na(v1.factor), v1.factor, v2.factor)

I suggest you work with the character levels first then convert it
into a factor, e.g. if v1.factor  v2.factor are already factors, do:

test.data$newvar - as.factor(ifelse(!is.na(v1.factor),
as.character(v1.factor), as.character(v2.factor)))



Regards,

Jon

__
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] If statement - copying a factor variable to a new variable

2012-06-28 Thread James Holland
Yeah, the reason I didn't use ifelse is because I've got multiple variables
to manipulate based on the if statement, some factors and some numeric.  I
have to look at the factor variables, and based on that, either use one
series of variables or another.

With the multiple if statements I need to check for, I though for
statements with the if/else if conditional statement was better than nested
ifelse functions.

For example

#example expanded on

rm(list=ls())

v1.factor - c(S,S,D,D,D,NA)
v2.factor - c(D,D,S,S,S,S)
v3 - c(1,0,0,0,0,NA)
v4 - c(0,0,1,1,0,0)

test.data - data.frame(v1.factor,v2.factor, v3, v4)

for (i in 1:nrow(test.data) ) {
   if ( (is.na(v1.factor[i])==TRUE) 
(is.na(v2.factor[i])==TRUE))
{test.data$newvar[i] - NA;

test.data$newvar2[i] - NA}#IF 1

  else if ( is.na(v1.factor[i])==TRUE 
is.na(v2.factor[i])==FALSE)
{test.data$newvar[i] - test.data$v1.factor[i];

 test.data$newvar2[i] - test.data$v4[i]} #IF 2

  else if ( is.na(v1.factor[i])==FALSE 
is.na(v2.factor[i])==TRUE)
{test.data$newvar[i] - test.data$v2.factor[i];

 test.data$newvar2[i] - test.data$v3[i]} #IF 3

  else if ( is.na(v1.factor[i])==FALSE 
is.na(v2.factor[i])==FALSE)
{test.data$newvar[i] - test.data$v1.factor[i];

  test.data$newvar2[i] - test.data$v4[i]} #IF 4

  } #End FOR

#End example

I'm not familiar with ifelse, but is there a way to use it in a nested
format that would be better than my for loop structure?  Or might I be
better off finding a programming way of converting the new factor variables
back to their factor values using the levels function?

Sorry for the questions, I'm self taught at R and still trying to learn the
best way to deal with these things.

James

On Thu, Jun 28, 2012 at 2:00 AM, Miguel Manese jjon...@gmail.com wrote:

 Hi James,

 On Thu, Jun 28, 2012 at 12:33 AM, James Holland holland.ag...@gmail.com
 wrote:
  I need to look through a dataset with two factor variables, and depending
  on certain criteria, create a new variable containing the data from one
 of
  those other variables.
 
  The problem is, R keeps making my new variable an integer and saving the
  data as a 1 or 2 (I believe the levels of the factor).
 
  I've tried using as.factor in the IF output statement, but that doesn't
  seem to work.
 
  Any help is appreciated.
 
 
 
  #Sample code
 
  rm(list=ls())
 
 
  v1.factor - c(S,S,D,D,D,NA)
  v2.factor - c(D,D,S,S,S,S)
 
  test.data - data.frame(v1.factor,v2.factor)

 The vectorized way to do that would be

 # v1.factor if present, else v2.factor
 test.data$newvar - ifelse(!is.na(v1.factor), v1.factor, v2.factor)

 I suggest you work with the character levels first then convert it
 into a factor, e.g. if v1.factor  v2.factor are already factors, do:

 test.data$newvar - as.factor(ifelse(!is.na(v1.factor),
 as.character(v1.factor), as.character(v2.factor)))



 Regards,

 Jon


[[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.


Re: [R] If statement - copying a factor variable to a new variable

2012-06-28 Thread Miguel Manese
On Thu, Jun 28, 2012 at 8:47 PM, James Holland holland.ag...@gmail.com wrote:
 With the multiple if statements I need to check for, I though for statements
 with the if/else if conditional statement was better than nested ifelse
 functions.

for () gives you a lot of flexibility at the expense of being verbose
 slow, ifelse() is a bit limited but you get conciseness (== more
elegant, IMO) and intuitively should be faster since it is vectorized

 For example

 #example expanded on

 rm(list=ls())

 v1.factor - c(S,S,D,D,D,NA)
 v2.factor - c(D,D,S,S,S,S)
 v3 - c(1,0,0,0,0,NA)
 v4 - c(0,0,1,1,0,0)

 test.data - data.frame(v1.factor,v2.factor, v3, v4)

Technically since you will pick a value from one of v1.factor,
v2.factor, v3, v4 into a new vector, they should have the same type
(e.g. numeric, character, integer). So I'll assume

v3 - c(S,D,D,D,D,NA)
v4 - c(D,D,S,S,D,D)

If you prefer vectorizing, you can create an index

# btw, is.na(v1.factor) is already logical (boolean),
# is.na(v1.factor)==TRUE is redundant
cond1 - is.na(v1.factor)  is.na(v2.factor)
cond2 - is.na(v1.factor)  ! is.na(v2.factor)
...

# cond1, cond2, etc should be mutually exclusive for this to work,
# i.e. for each row, one and only one of cond1, cond2, cond3 is TRUE
# not the case in your example, but you can make it so like
# cond2 - !cond1  (is.na(v1.factor)  !is.na(v2.factor))
# cond3 - !cond1  !cond2  (...)
idx - c(cond1, cond2, cond3, ...)

# to make it intuitive, you can convert idx into a matrix
# i.e. test.data[idx] will return elements of test.data corresponding
to elements of
# matrix idx which is TRUE
# this is actually optional,  R stores matrices in column-major order
idx - matrix(idx, nrow=length(cond1))

cbind(NA, test.data)[idx]# because your first condition should return NA!

Or you can use sapply(), which in essence is similar to for-loop().


 I'm not familiar with ifelse, but is there a way to use it in a nested
 format that would be better than my for loop structure?  Or might I be
 better off finding a programming way of converting the new factor variables
 back to their factor values using the levels function?

I don't understand your second question, but when combining factors it
is better to  deal with their labels (i.e. as.character(my.factor))
then convert the vector of strings to a factor (i.e.
as.factor(my.result)). Internally a factor is a vector of
(non-negative) integers, and levels(v1.factor) shows the mapping of
these integers to its label. So you'll have a problem e.g. if the
two factor vectors map the integer 1 to different labels.

Regards,

Jon

__
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] If statement - copying a factor variable to a new variable

2012-06-27 Thread James Holland
I need to look through a dataset with two factor variables, and depending
on certain criteria, create a new variable containing the data from one of
those other variables.

The problem is, R keeps making my new variable an integer and saving the
data as a 1 or 2 (I believe the levels of the factor).

I've tried using as.factor in the IF output statement, but that doesn't
seem to work.

Any help is appreciated.



#Sample code

rm(list=ls())


v1.factor - c(S,S,D,D,D,NA)
v2.factor - c(D,D,S,S,S,S)

test.data - data.frame(v1.factor,v2.factor)


for (i in 1:nrow(test.data) ) {
   if ( (is.na(v1.factor[i])==TRUE) 
(is.na(v2.factor[i])==TRUE))
{test.data$newvar[i] - NA}

  else if ( is.na(v1.factor[i])==TRUE 
is.na(v2.factor[i])==FALSE)
{test.data$newvar[i] - test.data$v1.factor[i]}

  else if ( is.na(v1.factor[i])==FALSE 
is.na(v2.factor[i])==TRUE)
{test.data$newvar[i] - test.data$v2.factor[i]}

  else if ( is.na(v1.factor[i])==FALSE 
is.na(v2.factor[i])==FALSE)
{test.data$newvar[i] - test.data$v1.factor[i]}

  } #End FOR


#Also, I just wrote this up quickly as sample code, but I'm not sure why my
6th case is coming up as NA when it should be going to the second IF
statement.
#End sample code



Thank you,

James

[[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.


Re: [R] conditional statement to replace values in dataframe with NA

2012-06-07 Thread peter dalgaard

On Jun 7, 2012, at 07:28 , Bert Gunter wrote:

 Actually, recycling makes the rep(NA,2) business unnecessary. Simply:
 
 dat1[dat1$x==1  dat1$y==1,1:2] - rep(NA,2)
 
 ##or
 
 with(dat1,{dat1[x==1  y==1,1:2] - NA;dat1})
 
 will do it.
 

Or, use the assignment form of is.na:

cond - with(dat1, x==1  y==1)
is.na(dat1$x) - cond
is.na(dat1$y) - cond

This is said to be somewhat safer if you are modifying factors (avoids 
potential confusion if NA is a level).

-pd


 -- Bert
 
 
 On Wed, Jun 6, 2012 at 10:21 PM, Bert Gunter bgun...@gene.com wrote:
 Have you read An Intro to R? If not,please do so before posting
 further. The way you are going about things makes me think you
 haven't, but ...
 
 This **is** a slightly tricky application of indexing, if I understand
 you correctly. Here are two essentially identical ways to do it, but
 the second is a little trickier
 
 ## First
 dat1[dat1$x==1  dat1$y==1,1:2] - rep(NA,2)
 dat1
 xy fac
 1 NA NA   A
 212   B
 313   A
 4 NA NA   C
 512   A
 613   C
 
 ##Slightly trickier version using with() to avoid explicit extraction
 from data frame
 ## Reconstitute dat1
 
 dat1
  x y fac
 1 1 1   C
 2 1 2   C
 3 1 3   B
 4 1 1   B
 5 1 2   C
 6 1 3   B
 
 dat1 - with(dat1,{dat1[x==1  y==1,1:2] - rep(NA,2); dat1})
 dat1
 xy fac
 1 NA NA   B
 212   A
 313   A
 4 NA NA   C
 512   A
 613   B
 
 ## ?with for explanation
 
 -- Bert
 
 On Wed, Jun 6, 2012 at 8:58 PM, Daisy Englert Duursma
 daisy.duur...@gmail.com wrote:
 Hello and thanks for helping.
 
 #some data
 L3 - LETTERS[1:3]
 dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, 
 replace=TRUE)))
 
 
 #When x==1 and y==1 I want to replace the 1 values with NA
 
 #I can select the rows I want:
 dat2-subset(dat1,x==1  y==1)
 #replace the 1 with NA
 dat2$x-rep(NA,nrow(dat2)
 dat2$y-rep(NA,nrow(dat2)
 
 #select the other rows and rbind everything back together
 #This is where I get stuck
 
 #The end dataframe will look something like:
 
   x y  fac
 NA NA   B
 NA NA   A
 1 2   C
 1 3   C
 1 2   C
 1 3   A
 
 #Is there a better way to do this where I do not need to subset
 perhaps using lapply?
 
 
 Thanks,
 Daisy
 
 --
 Daisy Englert Duursma
 Department of Biological Sciences
 Room E8C156
 Macquarie University, North Ryde, NSW 2109
 Australia
 
 Tel +61 2 9850 9256
 
 __
 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.
 
 
 
 --
 
 Bert Gunter
 Genentech Nonclinical Biostatistics
 
 Internal Contact Info:
 Phone: 467-7374
 Website:
 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
 
 
 
 -- 
 
 Bert Gunter
 Genentech Nonclinical Biostatistics
 
 Internal Contact Info:
 Phone: 467-7374
 Website:
 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
 
 __
 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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] conditional statement to replace values in dataframe with NA

2012-06-07 Thread arun
Hi,

Try this, dat1 - data.frame(x=rep(1,6),y=rep(1:3,2), fac=sample(L3, 6, 
replace=TRUE))
 dat1
  x y fac
1 1 1   C
2 1 2   B
3 1 3   B
4 1 1   A
5 1 2   B
6 1 3   B
 dat1[dat1$x==1dat1$y==1,1:2]-NA
 dat1
   x  y fac
1 NA NA   C
2  1  2   B
3  1  3   B
4 NA NA   A
5  1  2   B
6  1  3   B


A.K.



- Original Message -
From: Daisy Englert Duursma daisy.duur...@gmail.com
To: r-help@R-project.org r-help@r-project.org
Cc: 
Sent: Wednesday, June 6, 2012 11:58 PM
Subject: [R] conditional statement to replace values in dataframe with NA

Hello and thanks for helping.

#some data
L3 - LETTERS[1:3]
dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)))


#When x==1 and y==1 I want to replace the 1 values with NA

#I can select the rows I want:
dat2-subset(dat1,x==1  y==1)
#replace the 1 with NA
dat2$x-rep(NA,nrow(dat2)
dat2$y-rep(NA,nrow(dat2)

#select the other rows and rbind everything back together
#This is where I get stuck

#The end dataframe will look something like:

  x y  fac
NA NA   B
NA NA   A
1 2   C
1 3   C
1 2   C
1 3   A

#Is there a better way to do this where I do not need to subset
perhaps using lapply?


Thanks,
Daisy

--
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256

__
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.


[R] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Daisy Englert Duursma
Hello and thanks for helping.

#some data
L3 - LETTERS[1:3]
dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)))


#When x==1 and y==1 I want to replace the 1 values with NA

#I can select the rows I want:
dat2-subset(dat1,x==1  y==1)
#replace the 1 with NA
dat2$x-rep(NA,nrow(dat2)
dat2$y-rep(NA,nrow(dat2)

#select the other rows and rbind everything back together
#This is where I get stuck

#The end dataframe will look something like:

  x y  fac
NA NA   B
NA NA   A
1 2   C
1 3   C
1 2   C
1 3   A

#Is there a better way to do this where I do not need to subset
perhaps using lapply?


Thanks,
Daisy

--
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256

__
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] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Bert Gunter
Have you read An Intro to R? If not,please do so before posting
further. The way you are going about things makes me think you
haven't, but ...

This **is** a slightly tricky application of indexing, if I understand
you correctly. Here are two essentially identical ways to do it, but
the second is a little trickier

## First
 dat1[dat1$x==1  dat1$y==1,1:2] - rep(NA,2)
 dat1
 xy fac
1 NA NA   A
212   B
313   A
4 NA NA   C
512   A
613   C

##Slightly trickier version using with() to avoid explicit extraction
from data frame
## Reconstitute dat1

 dat1
  x y fac
1 1 1   C
2 1 2   C
3 1 3   B
4 1 1   B
5 1 2   C
6 1 3   B

dat1 - with(dat1,{dat1[x==1  y==1,1:2] - rep(NA,2); dat1})
 dat1
 xy fac
1 NA NA   B
212   A
313   A
4 NA NA   C
512   A
613   B

## ?with for explanation

-- Bert

On Wed, Jun 6, 2012 at 8:58 PM, Daisy Englert Duursma
daisy.duur...@gmail.com wrote:
 Hello and thanks for helping.

 #some data
 L3 - LETTERS[1:3]
 dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)))


 #When x==1 and y==1 I want to replace the 1 values with NA

 #I can select the rows I want:
 dat2-subset(dat1,x==1  y==1)
 #replace the 1 with NA
 dat2$x-rep(NA,nrow(dat2)
 dat2$y-rep(NA,nrow(dat2)

 #select the other rows and rbind everything back together
 #This is where I get stuck

 #The end dataframe will look something like:

   x y  fac
 NA NA   B
 NA NA   A
 1 2   C
 1 3   C
 1 2   C
 1 3   A

 #Is there a better way to do this where I do not need to subset
 perhaps using lapply?


 Thanks,
 Daisy

 --
 Daisy Englert Duursma
 Department of Biological Sciences
 Room E8C156
 Macquarie University, North Ryde, NSW 2109
 Australia

 Tel +61 2 9850 9256

 __
 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Bert Gunter
Actually, recycling makes the rep(NA,2) business unnecessary. Simply:

dat1[dat1$x==1  dat1$y==1,1:2] - rep(NA,2)

##or

with(dat1,{dat1[x==1  y==1,1:2] - NA;dat1})

will do it.

-- Bert


On Wed, Jun 6, 2012 at 10:21 PM, Bert Gunter bgun...@gene.com wrote:
 Have you read An Intro to R? If not,please do so before posting
 further. The way you are going about things makes me think you
 haven't, but ...

 This **is** a slightly tricky application of indexing, if I understand
 you correctly. Here are two essentially identical ways to do it, but
 the second is a little trickier

 ## First
 dat1[dat1$x==1  dat1$y==1,1:2] - rep(NA,2)
 dat1
     x    y fac
 1 NA NA   A
 2    1    2   B
 3    1    3   A
 4 NA NA   C
 5    1    2   A
 6    1    3   C

 ##Slightly trickier version using with() to avoid explicit extraction
 from data frame
 ## Reconstitute dat1

 dat1
  x y fac
 1 1 1   C
 2 1 2   C
 3 1 3   B
 4 1 1   B
 5 1 2   C
 6 1 3   B

 dat1 - with(dat1,{dat1[x==1  y==1,1:2] - rep(NA,2); dat1})
 dat1
     x    y fac
 1 NA NA   B
 2    1    2   A
 3    1    3   A
 4 NA NA   C
 5    1    2   A
 6    1    3   B

 ## ?with for explanation

 -- Bert

 On Wed, Jun 6, 2012 at 8:58 PM, Daisy Englert Duursma
 daisy.duur...@gmail.com wrote:
 Hello and thanks for helping.

 #some data
 L3 - LETTERS[1:3]
 dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)))


 #When x==1 and y==1 I want to replace the 1 values with NA

 #I can select the rows I want:
 dat2-subset(dat1,x==1  y==1)
 #replace the 1 with NA
 dat2$x-rep(NA,nrow(dat2)
 dat2$y-rep(NA,nrow(dat2)

 #select the other rows and rbind everything back together
 #This is where I get stuck

 #The end dataframe will look something like:

   x y  fac
 NA NA   B
 NA NA   A
 1 2   C
 1 3   C
 1 2   C
 1 3   A

 #Is there a better way to do this where I do not need to subset
 perhaps using lapply?


 Thanks,
 Daisy

 --
 Daisy Englert Duursma
 Department of Biological Sciences
 Room E8C156
 Macquarie University, North Ryde, NSW 2109
 Australia

 Tel +61 2 9850 9256

 __
 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.



 --

 Bert Gunter
 Genentech Nonclinical Biostatistics

 Internal Contact Info:
 Phone: 467-7374
 Website:
 http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] conditional statement to replace values in dataframe with NA

2012-06-06 Thread Daisy Englert Duursma
Thanks, problem solved.

On Thu, Jun 7, 2012 at 1:58 PM, Daisy Englert Duursma
daisy.duur...@gmail.com wrote:
 Hello and thanks for helping.

 #some data
 L3 - LETTERS[1:3]
 dat1 - data.frame(cbind(x=1, y=rep(1:3,2), fac=sample(L3, 6, replace=TRUE)))


 #When x==1 and y==1 I want to replace the 1 values with NA

 #I can select the rows I want:
 dat2-subset(dat1,x==1  y==1)
 #replace the 1 with NA
 dat2$x-rep(NA,nrow(dat2)
 dat2$y-rep(NA,nrow(dat2)

 #select the other rows and rbind everything back together
 #This is where I get stuck

 #The end dataframe will look something like:

   x y  fac
 NA NA   B
 NA NA   A
 1 2   C
 1 3   C
 1 2   C
 1 3   A

 #Is there a better way to do this where I do not need to subset
 perhaps using lapply?


 Thanks,
 Daisy

 --
 Daisy Englert Duursma
 Department of Biological Sciences
 Room E8C156
 Macquarie University, North Ryde, NSW 2109
 Australia

 Tel +61 2 9850 9256



-- 
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256

__
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] if statement

2012-03-07 Thread Val
Hi All,

I have one difficulty in using the conditional if statement

Assume ,

x - -1:4
 x
[1] -1  0  1  2  3  4

if x is lees than want I want to add 1 and I used the following command
 if(x0) {x=x+1}

Warning message:
In if (x  0) { :
  the condition has length  1 and only the first element will be used
 x
[1] 0 1 2 3 4 5
 That command added 1 to each element.

But I want like this  0 0 1 2 3 4

Can anybody help me?

Thanks
Val

[[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.


Re: [R] if statement

2012-03-07 Thread Sarah Goslee
You need ifelse() instead of if().

On Wed, Mar 7, 2012 at 2:12 PM, Val valkr...@gmail.com wrote:
 Hi All,

 I have one difficulty in using the conditional if statement

 Assume ,

 x - -1:4
  x
 [1] -1  0  1  2  3  4

 if x is lees than want I want to add 1 and I used the following command
  if(x0) {x=x+1}

 Warning message:
 In if (x  0) { :
  the condition has length  1 and only the first element will be used
 x
 [1] 0 1 2 3 4 5
  That command added 1 to each element.

 But I want like this  0 0 1 2 3 4

 Can anybody help me?

 Thanks
 Val

-- 
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] if statement

2012-03-07 Thread Jorge I Velez
Try

 ifelse( x  0, x + 1, x)
[1] 0 0 1 2 3 4

See also ?ifelse.

HTH,
Jorge.-


On Wed, Mar 7, 2012 at 2:12 PM, Val  wrote:

 Hi All,

 I have one difficulty in using the conditional if statement

 Assume ,

 x - -1:4
  x
 [1] -1  0  1  2  3  4

 if x is lees than want I want to add 1 and I used the following command
  if(x0) {x=x+1}

 Warning message:
 In if (x  0) { :
  the condition has length  1 and only the first element will be used
  x
 [1] 0 1 2 3 4 5
  That command added 1 to each element.

 But I want like this  0 0 1 2 3 4

 Can anybody help me?

 Thanks
 Val

[[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.


[[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.


Re: [R] if statement

2012-03-07 Thread Ted Harding
The simplest method would be:

  x[x0] - x[x0]+1

  x - -1:4
  x
  # [1] -1  0  1  2  3  4
  x[x0] - x[x0]+1
  x
  # [1] 0 0 1 2 3 4

I think where Val got confused is in thinking that

  if(x0)

is applied separately to each element of x, one at a time.

What actually happens, of course, is that x0 is evaluated
for each element of x (vectorially), giving

  x - -1:4
  x0
  # [1]  TRUE FALSE FALSE FALSE FALSE FALSE

and now the if(x0) x - x+1 is, in effect,

  if(c(TRUE,FALSE,FALSE,FALSE,FALSE,FALSE)) x - x+1

Hence the error message: if(...) expects a *single* TRUE
or FALSE argument, not a string of them; so, if given a string,
it picks the first, finds that it is TRUE, and so executes
  x - x+1
which adds 1 to every element of x.

From ?if:

  if(cond) expr
  cond: A length-one logical vector that is not 'NA'.
Conditions of length greater than one are accepted
with a warning, but only the first element is used.
Other types are coerced to logical if possible,
ignoring any class.

On the other hand, x[x0] selects those elements of x for
which x0 is TRUE, hence

  x[x0] - x[x0]+1

is a vectorial operation on those elements of x for which
x0 is TRUE, so each such element is replaced by 1 plus
that same element.

Hoping this helps,
Ted.

On 07-Mar-2012 Sarah Goslee wrote:
 You need ifelse() instead of if().
 
 On Wed, Mar 7, 2012 at 2:12 PM, Val valkr...@gmail.com wrote:
 Hi All,

 I have one difficulty in using the conditional if statement

 Assume ,

 x - -1:4
 x
 [1] -1 0 1 2 3 4

 if x is lees than want I want to add 1 and I used the following command
 if(x0) {x=x+1}

 Warning message:
 In if (x  0) { :
 the condition has length  1 and only the first element will be used
 x
 [1] 0 1 2 3 4 5
 That command added 1 to each element.

 But I want like this _0 0 1 2 3 4

 Can anybody help me?

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

-
E-Mail: (Ted Harding) ted.hard...@wlandres.net
Date: 07-Mar-2012  Time: 19:46:05
This message was sent by XFMail

__
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] if statement

2012-03-07 Thread AAsk
 x - -1:4
 x0 # returns TRUE (1) or FALSE (0)
[1]  TRUE FALSE FALSE FALSE FALSE FALSE
 x+as.numeric(x0)
[1] 0 0 1 2 3 4

__
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] if statement

2012-03-07 Thread Val
Now I got results as I wanted.
Thank you all.

On Wed, Mar 7, 2012 at 2:51 PM, AAsk aa2e...@lycos.co.uk wrote:

  x - -1:4
  x0 # returns TRUE (1) or FALSE (0)
 [1]  TRUE FALSE FALSE FALSE FALSE FALSE
  x+as.numeric(x0)
 [1] 0 0 1 2 3 4

 __
 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.


[[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.


[R] And Statement for two if functions

2012-01-30 Thread kerry1912
I want to perform two if functions at the same time:

if(home team  away team  home team = away team + 7) in R but i am
struggling to work out how to write this correctly. 

Thanks for any help. 

--
View this message in context: 
http://r.789695.n4.nabble.com/And-Statement-for-two-if-functions-tp4341179p4341179.html
Sent from the R help mailing list archive at Nabble.com.

__
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] And Statement for two if functions

2012-01-30 Thread Jorge I Velez
Hi kerry1912,

And what exactly would you like to do after the if(...) statement?  How did
you read your data in?  What's the output of str(yourdata)?  Please see
http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.htmland
help us to help you.

Regards,
Jorge


On Mon, Jan 30, 2012 at 9:52 AM, kerry1912  wrote:

 I want to perform two if functions at the same time:

 if(home team  away team  home team = away team + 7) in R but i am
 struggling to work out how to write this correctly.

 Thanks for any help.

 --
 View this message in context:
 http://r.789695.n4.nabble.com/And-Statement-for-two-if-functions-tp4341179p4341179.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


[[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.


Re: [R] And Statement for two if functions

2012-01-30 Thread David Winsemius


On Jan 30, 2012, at 9:52 AM, kerry1912 wrote:


I want to perform two if functions at the same time:

if(home team  away team  home team = away team + 7) in R but i am
struggling to work out how to write this correctly.


Generally newcomers to the R language find that the ifelse function  
does what they expect. The if function is quite different and seemes  
less likely to be what you wnat:


?Control
?ifelse


--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] And Statement for two if functions

2012-01-30 Thread kerry1912
Sorry that post was written in a bit if a rush.

I am writing a function in which I am trying to create a league table from a
data frame of rugby matches with the columns as follows: home team, away
team, home score and away score.

In rugby you can get an extra bonus point if you are the losing team and
lose by less than 7 points. So therefore in my function I am writing if the
away team loses AND loses by less than or equal to 7 points then the away
team will get an extra point, 

So ideally want to write:

if(games[i,3]  games[i,4]  AND games[i,3] = games[i,4] + 7) {
T[which(teams == games[i,2]),Points] -
T[which(teams == 
games[i,2]),Points] + 1}

Which is inset into a function in R where the input of the function is
'games' which will be the list of the 132 matches of rugby being analysed
and where teams is the list of 12 teams in the league. 

I wasn't sure if it was possible to write an 'if' function embedded in
another 'if' function or which method would be best to achieve this. 

Thank you. 

 


--
View this message in context: 
http://r.789695.n4.nabble.com/And-Statement-for-two-if-functions-tp4341179p4342098.html
Sent from the R help mailing list archive at Nabble.com.

__
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] And Statement for two if functions

2012-01-30 Thread R. Michael Weylandt
Nested if's are fine in R, but as David said you probably want
ifelse(). This sounds sufficiently homework-y that I'm hesitant to
give example code but it's all over the archives.

Just to head off a problem I see in your pesudo-code; you're going to
want to use ifelse() to construct the points vector and then assign
it: it's terribly dangerous to do assignment within ifelse() as if it
were a simple if().

Michael


On Mon, Jan 30, 2012 at 1:55 PM, kerry1912 kerry1...@hotmail.com wrote:
 Sorry that post was written in a bit if a rush.

 I am writing a function in which I am trying to create a league table from a
 data frame of rugby matches with the columns as follows: home team, away
 team, home score and away score.

 In rugby you can get an extra bonus point if you are the losing team and
 lose by less than 7 points. So therefore in my function I am writing if the
 away team loses AND loses by less than or equal to 7 points then the away
 team will get an extra point,

 So ideally want to write:

 if(games[i,3]  games[i,4]  AND games[i,3] = games[i,4] + 7) {
                                T[which(teams == games[i,2]),Points] -
                                                        T[which(teams == 
 games[i,2]),Points] + 1}

 Which is inset into a function in R where the input of the function is
 'games' which will be the list of the 132 matches of rugby being analysed
 and where teams is the list of 12 teams in the league.

 I wasn't sure if it was possible to write an 'if' function embedded in
 another 'if' function or which method would be best to achieve this.

 Thank you.




 --
 View this message in context: 
 http://r.789695.n4.nabble.com/And-Statement-for-two-if-functions-tp4341179p4342098.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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] if statement problem

2011-12-25 Thread Uwe Ligges



On 24.12.2011 12:03, reena wrote:

It didn't work. :(


What did not work???

Please do not misuse the R-help mailing list! Its posting guide clearly 
asks you to cite the thread and specify reproducible examples that make 
other able to help.


Best,
Uwe Ligges





--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html
Sent from the R help mailing list archive at Nabble.com.

__
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] if statement problem

2011-12-25 Thread Rui Barradas
Hello again.

I don't understand what didn't work.

First, it seems better to use 'nrow', the result is the same

 stopifnot(length(x[,1]) == nrow(x))

Then your multiple  OR condition.

#if((x[i,1] || x[i,2] || x[i,3] || x[i,4])  5)

x - matrix(1:24, ncol=4)

for(i in 1:nrow(x))
if(any(x[i,]  5))
cat(At least one is TRUE:, i, \n)

for(i in 1:nrow(x))
if((x[i,1]  5) | (x[i,2]  5) | (x[i,3]  5) | (x[i,4]  5))
cat(The same with '|':, i, \n)

In the second example each condition IS a condition. Unlike your original
compound one.
The 'Introduction to R' clearly states '|' as the union of the logical
expressions, The section is 2.4.
Also read Patrick's PDF, the parts I've read are great and very usefull.

(Thanks, Patrick, I was unaware of R inferno.pdf)

As a side note, you are duplicating the matrix 'a' assignement. Why not just
before the 'if'?
Then, you could simply test if any 'a' is less than 5.

for (i in 1: row(x))
{
   a[1,1]  - x[i,1];
   a[1,2] - x[i,2];
   a[2,1] - x[i,3];
   a[2,2] - x[i,4];
   if(any(a  5))
 {
etc...
   
(Or use the compound '|' ).

Rui Barradas

--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4233312.html
Sent from the R help mailing list archive at Nabble.com.

__
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] if statement problem

2011-12-24 Thread Patrick Burns

This is almost Circle 8.1.7 of
'The R Inferno':

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

but is making the mistake in the
other direction.

On 23/12/2011 22:40, reena wrote:

Hello,

I want to do fisher test for the rows in data file which has value less than
5 otherwise chi square test .The p values from both test should be stored in
one resulted file. but there is some problem with bold if statement. I don't
know how
implement this line properly.


x = cbind(obs1,obs2,exp1,exp2)
a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized
values

for (i in 1: length(x[,1]))
{
   *if((x[i,1] || x[i,2] || x[i,3] || x[i,4])  5)*
  {
  a[1,1]- x[i,1];
  a[1,2]- x[i,2];
  a[2,1]- x[i,3];
  a[2,2]- x[i,4];
  result- fisher.test(a)
  write.table(result[[p.value]],file=results.txt,
sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);
}

  else
 {
 a[1,1]- x[i,1];
 a[1,2]- x[i,2];
 a[2,1]- x[i,3];
 a[2,2]- x[i,4];
 result- chisq.test(a)
 write.table(result[[p.value]],file=results.txt,
sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);}
 }

Regards
R

--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html
Sent from the R help mailing list archive at Nabble.com.

__
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.



--
Patrick Burns
pbu...@pburns.seanet.com
twitter: @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')

__
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] if statement problem

2011-12-24 Thread reena
It didn't work. :(

--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230933.html
Sent from the R help mailing list archive at Nabble.com.

__
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] if statement problem

2011-12-23 Thread reena
Hello,

I want to do fisher test for the rows in data file which has value less than
5 otherwise chi square test .The p values from both test should be stored in
one resulted file. but there is some problem with bold if statement. I don't
know how 
implement this line properly.


x = cbind(obs1,obs2,exp1,exp2)
a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with initialized
values

for (i in 1: length(x[,1]))
{
  *if((x[i,1] || x[i,2] || x[i,3] || x[i,4])  5)*
 {
 a[1,1]  - x[i,1];
 a[1,2] - x[i,2];
 a[2,1] - x[i,3];
 a[2,2] - x[i,4];
 result - fisher.test(a)
 write.table(result[[p.value]],file=results.txt,
sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);
   }

 else
{
a[1,1] - x[i,1];
a[1,2] - x[i,2];
a[2,1] - x[i,3];
a[2,2] - x[i,4];
result - chisq.test(a)
write.table(result[[p.value]],file=results.txt,
sep=\n, append=TRUE, col.names=FALSE, row.names=FALSE);}
}

Regards
R

--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230026.html
Sent from the R help mailing list archive at Nabble.com.

__
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] if statement problem

2011-12-23 Thread Rui Barradas

reena wrote
 
 Hello,
 
 I want to do fisher test for the rows in data file which has value less
 than 5 otherwise chi square test .The p values from both test should be
 stored in one resulted file. but there is some problem with bold if
 statement. I don't know how 
 implement this line properly.
 
 
 x = cbind(obs1,obs2,exp1,exp2)
 a = matrix(c(0,0,0,0), ncol=2, byrow =TRUE)#matrix with
 initialized values
 
 for (i in 1: length(x[,1]))
 {
   *if((x[i,1] || x[i,2] || x[i,3] || x[i,4])  5)*

 

Hello,
Try

*if(any(x[i,] 5))*

Merry Christmas
Rui Barradas



--
View this message in context: 
http://r.789695.n4.nabble.com/if-statement-problem-tp4230026p4230135.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Ifelse statement

2011-07-11 Thread fre
Hello everyone,

I have a (small) issue. I already googled a lot, so I decided to use ifelse
instead of if (){} else{}

All the elements seem to work seperately, but combined in the ifelse
statement, it doesn't seem to work.

#The price function is a function which is normally distributed with only
positive answers
price-function() {abs(rnorm(1,10,25))}

#Before I use pieceprice in the ifelse, I need it to be defined
pieceprice-cbind()

#Now I define a function with an ifelse statement. So if the binomial
deviation returns one, I add a 'new' price() to the pieceprice vector else I
repeat the last element of the pieceprice vector and add it to pieceprice.
pricechange-function()
{ifelse(rbinom(1,1,2/3)==1,
pieceprice-cbind(pieceprice,price()),
pieceprice-cbind(pieceprice,pieceprice[1,length(pieceprice)]))}

#But now if I try this (Even with rbinom(1,1,1), pieceprice remains NULL
pricechange()
pieceprice


 pricechange()
[1] 79.20426
 pieceprice
NULL


I hope someone has a clue what's wrong.

Thanks a lot for your help!


--
View this message in context: 
http://r.789695.n4.nabble.com/Ifelse-statement-tp3660627p3660627.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Ifelse statement

2011-07-11 Thread Sarah Goslee
Hi,

You've got several things going on here.

On Mon, Jul 11, 2011 at 3:39 PM, fre fre_stam...@hotmail.com wrote:
 Hello everyone,

 I have a (small) issue. I already googled a lot, so I decided to use ifelse
 instead of if (){} else{}

 All the elements seem to work seperately, but combined in the ifelse
 statement, it doesn't seem to work.

 #The price function is a function which is normally distributed with only
 positive answers
 price-function() {abs(rnorm(1,10,25))}

 #Before I use pieceprice in the ifelse, I need it to be defined
 pieceprice-cbind()

This isn't true. You aren't passing pieceprice to your function, so
you don't need to define it first.

So you are creating an empty object pieceprice here.


 #Now I define a function with an ifelse statement. So if the binomial
 deviation returns one, I add a 'new' price() to the pieceprice vector else I
 repeat the last element of the pieceprice vector and add it to pieceprice.
 pricechange-function()
 {ifelse(rbinom(1,1,2/3)==1,
 pieceprice-cbind(pieceprice,price()),
 pieceprice-cbind(pieceprice,pieceprice[1,length(pieceprice)]))}

 #But now if I try this (Even with rbinom(1,1,1), pieceprice remains NULL
 pricechange()
 pieceprice


Now you're doing some stuff in your function, and returning a value.

 pricechange()
 [1] 79.20426

But you're not assigning your value to anything, so it's being printed
in the screen. Assigning it *within the function* by design and
default does not change anything in the outer environment.

 pieceprice
 NULL

You didn't change pieceprice, so it hasn't changed.

pieceprice - pricechange()

will run the function and assign its output to the object pieceprice,
which doesn't have to already exist.


 I hope someone has a clue what's wrong.

 Thanks a lot for your help!

Leaving aside the utility of functions that only ever return one
possible value, you would probably benefit from reading some
introductory material on writing functions.

Sarah

-- 
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.


[R] If Statement

2011-03-08 Thread dpender
Hi,

I am having some problems using the if statement correctly.  I have used it
many times previously so I dona't know what is different with this case. 
Here is my problem:

I have a 1X10 matrix of values as follows:

 H.MC
  [,1]
 [1,] 4.257669
 [2,] 7.023242
 [3,] 4.949857
 [4,] 5.107000
 [5,] 4.257669
 [6,] 4.257669
 [7,] 4.257669
 [8,] 4.257669
 [9,] 4.257669
[10,] 4.257669

What I want to do is replace all the values if 4.257669 with a random number
between 3 and 4.5.  To do this I have:

H.MC.fin - matrix(0,10,1)

for (j in 1:10) {

if(H.MC[j] == 4.257669) H.MC.fin[j] -runif(1,3,4.5) else H.MC.fin[j] -
H.MC[j] 

}

This doesn't seem to do anything and H.MC.fin is the same as H.MC.

Does anyone know what I am doing wrong?

Thanks,

Doug


--
View this message in context: 
http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html
Sent from the R help mailing list archive at Nabble.com.

__
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] If Statement

2011-03-08 Thread andrija djurovic
Hi,

matrix has a 2 dimensions.

Is this work:

a-matrix(rep(c(1,2),c(5,5)),ncol=1)
dim(a)
for (i in 1:10)
{
ifelse(a[i,]==1, a[i,]-runif(1,3,4.5), a[i,])
}
a

Andrija


On Tue, Mar 8, 2011 at 1:07 PM, dpender d.pende...@research.gla.ac.ukwrote:

 Hi,

 I am having some problems using the if statement correctly.  I have used it
 many times previously so I dona't know what is different with this case.
 Here is my problem:

 I have a 1X10 matrix of values as follows:

  H.MC http://h.mc/
  [,1]
  [1,] 4.257669
  [2,] 7.023242
  [3,] 4.949857
  [4,] 5.107000
  [5,] 4.257669
  [6,] 4.257669
  [7,] 4.257669
  [8,] 4.257669
  [9,] 4.257669
 [10,] 4.257669

 What I want to do is replace all the values if 4.257669 with a random
 number
 between 3 and 4.5.  To do this I have:

 H.MC.fin - matrix(0,10,1)

 for (j in 1:10) {

 if(H.MC http://h.mc/[j] == 4.257669) H.MC.fin[j] -runif(1,3,4.5) else
 H.MC.fin[j] -
 H.MC http://h.mc/[j]

 }

 This doesn't seem to do anything and H.MC.fin is the same as 
 H.MChttp://h.mc/
 .

 Does anyone know what I am doing wrong?

 Thanks,

 Doug


 --
 View this message in context:
 http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.htmlhttp://www.r-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[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.


Re: [R] If Statement

2011-03-08 Thread David Winsemius


On Mar 8, 2011, at 7:07 AM, dpender wrote:


Hi,

I am having some problems using the if statement correctly.  I have  
used it
many times previously so I dona't know what is different with this  
case.

Here is my problem:

I have a 1X10 matrix of values as follows:


H.MC

 [,1]
[1,] 4.257669
[2,] 7.023242
[3,] 4.949857
[4,] 5.107000
[5,] 4.257669
[6,] 4.257669
[7,] 4.257669
[8,] 4.257669
[9,] 4.257669
[10,] 4.257669

What I want to do is replace all the values if 4.257669 with a  
random number

between 3 and 4.5.  To do this I have:

H.MC.fin - matrix(0,10,1)

for (j in 1:10) {

if(H.MC[j] == 4.257669)


Testing for equality of floating point numbers is a common source of  
errors. See the FAQ.


--
David.

H.MC.fin[j] -runif(1,3,4.5) else H.MC.fin[j] -
H.MC[j]

}

This doesn't seem to do anything and H.MC.fin is the same as H.MC.

Does anyone know what I am doing wrong?

Thanks,

Doug


--
View this message in context: 
http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


David Winsemius, MD
West Hartford, CT

__
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] If Statement

2011-03-08 Thread Petr Savicky
On Tue, Mar 08, 2011 at 04:07:03AM -0800, dpender wrote:
 Hi,
 
 I am having some problems using the if statement correctly.  I have used it
 many times previously so I dona't know what is different with this case. 
 Here is my problem:
 
 I have a 1X10 matrix of values as follows:
 
  H.MC
   [,1]
  [1,] 4.257669
  [2,] 7.023242
  [3,] 4.949857
  [4,] 5.107000
  [5,] 4.257669
  [6,] 4.257669
  [7,] 4.257669
  [8,] 4.257669
  [9,] 4.257669
 [10,] 4.257669
 
 What I want to do is replace all the values if 4.257669 with a random number
 between 3 and 4.5.  To do this I have:
 
 H.MC.fin - matrix(0,10,1)
 
 for (j in 1:10) {
 
 if(H.MC[j] == 4.257669) H.MC.fin[j] -runif(1,3,4.5) else H.MC.fin[j] -
 H.MC[j] 
 
 }
 
 This doesn't seem to do anything and H.MC.fin is the same as H.MC.

Hi.

It is likely that none of the numbers in H.MC is exactly 4.257669.
This is easy to check by printing

  H.MC - 4.257669

The condition H.MC[j] == 4.257669 is satisfied for those numbers, which
are printed as 0. If there are numbers close to 4.257669, but not
exactly equal, try to determine the maximum difference from 4.257669,
which should still be interpreted as equality.

The function all.equal(), which is suggested for comparison of numeric
values, reports the numbers as equal, if their relative difference is
at most (approx) 1.5e-8. Use it, for example, as

  isTRUE(all.equal(H.MC[j], 4.257669))

If an absolute error is more suitable, try using

  abs(H.MC[j] - 4.257669)  1e-10

with a possibly different bound.

Hope this helps.

Petr Savicky.

__
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] If Statement

2011-03-08 Thread William Dunlap
 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of dpender
 Sent: Tuesday, March 08, 2011 4:07 AM
 To: r-help@r-project.org
 Subject: [R] If Statement
 
 Hi,
 
 I am having some problems using the if statement correctly.  
 I have used it
 many times previously so I dona't know what is different with 
 this case. 
 Here is my problem:
 
 I have a 1X10 matrix of values as follows:
 
  H.MC
   [,1]
  [1,] 4.257669
  [2,] 7.023242
  [3,] 4.949857
  [4,] 5.107000
  [5,] 4.257669
  [6,] 4.257669
  [7,] 4.257669
  [8,] 4.257669
  [9,] 4.257669
 [10,] 4.257669
 
 What I want to do is replace all the values if 4.257669 with 
 a random number
 between 3 and 4.5.  To do this I have:
 
 H.MC.fin - matrix(0,10,1)
 
 for (j in 1:10) {
 
 if(H.MC[j] == 4.257669) H.MC.fin[j] -runif(1,3,4.5) else 
 H.MC.fin[j] -
 H.MC[j] 
 
 }

Your for loop can be replaced by:
   whichToReplace - H.MC == 4.257669
   H.MC.fin - H.MC # copy the whole thing
   # then replace some values in the copy
   H.MC.fin[whichToReplace] - runif(sum(whichToReplace), 3, 45)
whichToReplace will be a logical vector the length of H.MC,
with a TRUE value at the positions to be replaced.  Summing a
logical vector gives the number of TRUE's in it.

Others have pointed out that the initial test would be better
done by something like
   whichToReplace - abs(H.MC - 4.257669)  0.1
instead of demanding exact equality.

If there might be NA's in H.MC, following the test with
   whichToReplace - !is.na(whichToReplace)  whichToReplace

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 This doesn't seem to do anything and H.MC.fin is the same as H.MC.
 
 Does anyone know what I am doing wrong?
 
 Thanks,
 
 Doug
 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/If-Statement-tp3341167p3341167.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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.


[R] if statement and truncated distribution

2010-10-25 Thread Sally Luo
Hi R helpers,

I am trying to use the if statement to generate a truncated random variable
as follows:

if (y[i]==0)  { v[i] ~ rnorm(1,0,1) | (-inf ,0) }
if (y[i]==1) { v[i] ~ rnorm(1,0,1) | (0, inf) }

I guess I cannot use  | (  , )  to restrict the range of a variable in R.
Could you let me know how to write the code correctly in R?

Many thanks for your help.

Maomao

[[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.


Re: [R] if statement and truncated distribution

2010-10-25 Thread Nick Sabbe
What I guess you want is something like (this is for zero-truncation):

rZeroTruncNormal1d-function(mu, sig, invalidSign) #sig holds standard
deviation!
{
val-rnorm(1, mu, sig)
while(val * invalidSign  0)
{
val-rnorm(1, mu, sig)
}
return(val)
}


Nick Sabbe
--
ping: nick.sa...@ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Sally Luo
Sent: maandag 25 oktober 2010 2:01
To: r-help@r-project.org
Subject: [R] if statement and truncated distribution

Hi R helpers,

I am trying to use the if statement to generate a truncated random variable
as follows:

if (y[i]==0)  { v[i] ~ rnorm(1,0,1) | (-inf ,0) }
if (y[i]==1) { v[i] ~ rnorm(1,0,1) | (0, inf) }

I guess I cannot use  | (  , )  to restrict the range of a variable in R.
Could you let me know how to write the code correctly in R?

Many thanks for your help.

Maomao

[[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.

__
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] If Statement Help

2010-10-23 Thread Jason Kwok
Price
2010-10-11 99
2010-10-12101
2010-10-13102
2010-10-14103
2010-10-15 99
2010-10-18 98
2010-10-19 97
2010-10-20101
2010-10-21101
2010-10-22101

I have this dataset and I only want to return instances when the Price is 
100.

If I use the code: Price  100 then it will evaluate each entry as TRUE or
FALSE.  What is the code to only return TRUE results?

Thanks,

Jay

[[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.


Re: [R] If Statement Help

2010-10-23 Thread jim holtman
Need to understand how 'indexing' is done in R:

 x - read.table(textConnection(   Price
+ 2010-10-11 99
+ 2010-10-12101
+ 2010-10-13102
+ 2010-10-14103
+ 2010-10-15 99
+ 2010-10-18 98
+ 2010-10-19 97
+ 2010-10-20101
+ 2010-10-21101
+ 2010-10-22101), header = TRUE)
 closeAllConnections()
 x
   Price
2010-10-1199
2010-10-12   101
2010-10-13   102
2010-10-14   103
2010-10-1599
2010-10-1898
2010-10-1997
2010-10-20   101
2010-10-21   101
2010-10-22   101
 x[x$Price  100,, drop = FALSE]
   Price
2010-10-12   101
2010-10-13   102
2010-10-14   103
2010-10-20   101
2010-10-21   101
2010-10-22   101



On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok jayk...@gmail.com wrote:
                    Price
 2010-10-11     99
 2010-10-12    101
 2010-10-13    102
 2010-10-14    103
 2010-10-15     99
 2010-10-18     98
 2010-10-19     97
 2010-10-20    101
 2010-10-21    101
 2010-10-22    101

 I have this dataset and I only want to return instances when the Price is 
 100.

 If I use the code: Price  100 then it will evaluate each entry as TRUE or
 FALSE.  What is the code to only return TRUE results?

 Thanks,

 Jay

        [[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.




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

What is the problem that you are trying to solve?

__
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] If Statement Help

2010-10-23 Thread Jorge Ivan Velez
Hi Jay,

If x is your data, you could use subset() to do what you want:

subset(x, Price  100)

See ?subset for more information.

HTH,
Jorge


On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok  wrote:

Price
 2010-10-11 99
 2010-10-12101
 2010-10-13102
 2010-10-14103
 2010-10-15 99
 2010-10-18 98
 2010-10-19 97
 2010-10-20101
 2010-10-21101
 2010-10-22101

 I have this dataset and I only want to return instances when the Price is 
 100.

 If I use the code: Price  100 then it will evaluate each entry as TRUE
 or
 FALSE.  What is the code to only return TRUE results?

 Thanks,

 Jay

[[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.


[[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.


Re: [R] If Statement Help

2010-10-23 Thread Jason Kwok
Thanks for the help Jim.

As a new user and member of this mailing list, I'm very impressed with all
the support!

Jay

On Sat, Oct 23, 2010 at 10:21 PM, jim holtman jholt...@gmail.com wrote:

 Need to understand how 'indexing' is done in R:

  x - read.table(textConnection(   Price
 + 2010-10-11 99
 + 2010-10-12101
 + 2010-10-13102
 + 2010-10-14103
 + 2010-10-15 99
 + 2010-10-18 98
 + 2010-10-19 97
 + 2010-10-20101
 + 2010-10-21101
 + 2010-10-22101), header = TRUE)
  closeAllConnections()
  x
   Price
 2010-10-1199
 2010-10-12   101
 2010-10-13   102
 2010-10-14   103
 2010-10-1599
 2010-10-1898
 2010-10-1997
 2010-10-20   101
 2010-10-21   101
 2010-10-22   101
  x[x$Price  100,, drop = FALSE]
   Price
 2010-10-12   101
 2010-10-13   102
 2010-10-14   103
 2010-10-20   101
 2010-10-21   101
 2010-10-22   101
 


 On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok jayk...@gmail.com wrote:
 Price
  2010-10-11 99
  2010-10-12101
  2010-10-13102
  2010-10-14103
  2010-10-15 99
  2010-10-18 98
  2010-10-19 97
  2010-10-20101
  2010-10-21101
  2010-10-22101
 
  I have this dataset and I only want to return instances when the Price is
 
  100.
 
  If I use the code: Price  100 then it will evaluate each entry as TRUE
 or
  FALSE.  What is the code to only return TRUE results?
 
  Thanks,
 
  Jay
 
 [[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.
 



 --
 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.


Re: [R] If Statement Help

2010-10-23 Thread Jason Kwok
Thanks Jorge.  It works.

Is there a way to keep the actual price in the price column instead of
TRUE/FALSE but filtering on when price100?

Thanks,

Jay

On Sat, Oct 23, 2010 at 10:37 PM, Jorge Ivan Velez jorgeivanve...@gmail.com
 wrote:

 Hi Jay,

 If x is your data, you could use subset() to do what you want:

 subset(x, Price  100)

 See ?subset for more information.

 HTH,
 Jorge


 On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok  wrote:

Price
 2010-10-11 99
 2010-10-12101
 2010-10-13102
 2010-10-14103
 2010-10-15 99
 2010-10-18 98
 2010-10-19 97
 2010-10-20101
 2010-10-21101
 2010-10-22101

 I have this dataset and I only want to return instances when the Price is
 
 100.

 If I use the code: Price  100 then it will evaluate each entry as TRUE
 or
 FALSE.  What is the code to only return TRUE results?

 Thanks,

 Jay

[[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.




[[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.


Re: [R] If Statement Help

2010-10-23 Thread David Winsemius


On Oct 23, 2010, at 7:44 PM, Jason Kwok wrote:


Thanks Jorge.  It works.

Is there a way to keep the actual price in the price column instead of
TRUE/FALSE but filtering on when price100?


Huh? When I use subset I get what you ask for:

 subset(x, Price  100)
   Price
2010-10-12   101
2010-10-13   102
2010-10-14   103
2010-10-20   101
2010-10-21   101
2010-10-22   101



Thanks,

Jay

On Sat, Oct 23, 2010 at 10:37 PM, Jorge Ivan Velez jorgeivanve...@gmail.com

wrote:



Hi Jay,

If x is your data, you could use subset() to do what you want:

subset(x, Price  100)

See ?subset for more information.

HTH,
Jorge


On Sat, Oct 23, 2010 at 9:56 PM, Jason Kwok  wrote:


  Price
2010-10-11 99
2010-10-12101
2010-10-13102
2010-10-14103
2010-10-15 99
2010-10-18 98
2010-10-19 97
2010-10-20101
2010-10-21101
2010-10-22101

I have this dataset and I only want to return instances when the  
Price is



100.

If I use the code: Price  100 then it will evaluate each entry as  
TRUE

or
FALSE.  What is the code to only return TRUE results?

Thanks,

Jay

  [[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.






[[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.


__
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] If Statement with more than one condition

2010-10-22 Thread Santosh Srinivas
I'm unable to  find the OR operator like other language .. any suggestions?

I want to do If (condition1 OR condition 2){ do something }

 

Thanks for answering this elementary question.

 


[[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.


Re: [R] If Statement with more than one condition

2010-10-22 Thread Joshua Wiley
Hi Santosh,

I believe you are looking for |.  For example:

if(3  5 | 3  4) {print(TRUE)}

Cheers,

Josh

On Fri, Oct 22, 2010 at 12:51 AM, Santosh Srinivas
santosh.srini...@gmail.com wrote:
 I'm unable to  find the OR operator like other language .. any suggestions?

 I want to do If (condition1 OR condition 2){ do something }



 Thanks for answering this elementary question.




        [[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.




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
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] If Statement with more than one condition

2010-10-22 Thread Duncan Murdoch

Joshua Wiley wrote:

Hi Santosh,

I believe you are looking for |.  For example:

if(3  5 | 3  4) {print(TRUE)}


In an if () statement you use || more often.  | is a vector operator 
that always evaluates both arguments; || is a scalar operator that quits 
if the left hand argument determines the result.


Duncan Murdoch



Cheers,

Josh

On Fri, Oct 22, 2010 at 12:51 AM, Santosh Srinivas
santosh.srini...@gmail.com wrote:

I'm unable to  find the OR operator like other language .. any suggestions?

I want to do If (condition1 OR condition 2){ do something }



Thanks for answering this elementary question.




   [[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.







__
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] If Statement with more than one condition

2010-10-22 Thread news
Santosh Srinivas santosh.srini...@gmail.com writes:

 I'm unable to  find the OR operator like other language .. any suggestions?

 I want to do If (condition1 OR condition 2){ do something }

if((condition1) | (condition2)){ do something }

-- 
aleblanc

__
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] ifelse statement

2010-09-17 Thread n.via...@libero.it
Dear list,
I have a question I'm trying to use the following command in R, but it gives 
me an error message.The command is:

data-ddply(data,c(year,name), transform, check1=ifelse(check1==1  
check2==1, 1,NULL))

so in my data frame I already have the check1 variable, if the conditions 
(check1==1  check2==1) is respected, check has to have 1 as value, whereas if 
it is not I want that variable to maintain its value.
Anyone knows, how to do this, by doing this for name and year??

__
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] ifelse statement

2010-09-17 Thread Ivan Calandra
  Hi,

Not sure since I've never done it, but shouldn't it be NA instead of NULL?

Ivan

Le 9/17/2010 15:23, n.via...@libero.it a écrit :
 Dear list,
 I have a question I'm trying to use the following command in R, but it gives
 me an error message.The command is:

 data-ddply(data,c(year,name), transform, check1=ifelse(check1==1
 check2==1, 1,NULL))

 so in my data frame I already have the check1 variable, if the conditions
 (check1==1  check2==1) is respected, check has to have 1 as value, whereas if
 it is not I want that variable to maintain its value.
 Anyone knows, how to do this, by doing this for name and year??

 __
 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.


-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[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.


[R] any statement equals to 'goto'?

2010-08-31 Thread karena

I have the following code:
-
result - matrix(NA, nrow=1, ncol=5)
for(i in 1:(nsnp-1)) {
  for(j in (i+1):nsnp){
tempsnp1 - data.lme[,i]
tempsnp2 - data.lme[,j]
fm1 - lme(trait~sex+age+rmtemp.b+fc+tempsnp1+tempsnp2+tempsnp1*tempsnp2,
random=~1|famid, na.action=na.omit)
fm2 - lme(trait~sex+age+rmtemp.b+fc+tempsnp1+tempsnp2, random=~1|famid,
na.action=na.omit)
result[1,1] - i
result[1,2] - j
result[1,3] - colnames(data.lme)[i]
result[1,4] - colnames(data.lme)[j]
result[1,5] - 2*(summary(fm1)$logLik-summary(fm2)$logLik)
if (i==1  j==2) results - result
if (i!=1 | j!=2) results - rbind(results, result)
}
-

after submitting this code, I got the error message saying Error in
MEEM(object, conLin, control$niterEM) : 
Singularity in backsolve at level 0, block 1, I know this might be some
issue due to the missing values. what I want to do is to skip the 'bad'
variable, for example, I got this error message when j=116, so I just wanna
ignore variable116, is there a statement that can do this: when getting this
error message 'Error in MEEM...', directly goto the next variable?

thank you,

karena



-- 
View this message in context: 
http://r.789695.n4.nabble.com/any-statement-equals-to-goto-tp2402107p2402107.html
Sent from the R help mailing list archive at Nabble.com.

__
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] any statement equals to 'goto'?

2010-08-31 Thread Allan Engelhardt

Sounds like you want

help(tryCatch) # Catch the error and do something
help(next)  # Go to the next value of the surrounding loop

Hope this helps a little.

Allan

On 31/08/10 19:34, karena wrote:

I have the following code:
-
result- matrix(NA, nrow=1, ncol=5)
for(i in 1:(nsnp-1)) {
   for(j in (i+1):nsnp){
tempsnp1- data.lme[,i]
tempsnp2- data.lme[,j]
fm1- lme(trait~sex+age+rmtemp.b+fc+tempsnp1+tempsnp2+tempsnp1*tempsnp2,
random=~1|famid, na.action=na.omit)
fm2- lme(trait~sex+age+rmtemp.b+fc+tempsnp1+tempsnp2, random=~1|famid,
na.action=na.omit)
result[1,1]- i
result[1,2]- j
result[1,3]- colnames(data.lme)[i]
result[1,4]- colnames(data.lme)[j]
result[1,5]- 2*(summary(fm1)$logLik-summary(fm2)$logLik)
if (i==1  j==2) results- result
if (i!=1 | j!=2) results- rbind(results, result)
}
-

after submitting this code, I got the error message saying Error in
MEEM(object, conLin, control$niterEM) :
Singularity in backsolve at level 0, block 1, I know this might be some
issue due to the missing values. what I want to do is to skip the 'bad'
variable, for example, I got this error message when j=116, so I just wanna
ignore variable116, is there a statement that can do this: when getting this
error message 'Error in MEEM...', directly goto the next variable?

thank you,

karena






__
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] any statement equals to 'goto'?

2010-08-31 Thread karena

these seem something that I am looking for, I will try them, thank you!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/any-statement-equals-to-goto-tp2402107p2402264.html
Sent from the R help mailing list archive at Nabble.com.

__
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] ifelse statement

2010-07-07 Thread karena

Hi, I am a newbie of R, and playing with the ifelse statement.

I have the following codes:
## first,

for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
if(i==1  j==2) corr.iris - cor.temp
else corr.iris - c(corr.iris, cor.temp)
}
}

this code is working fine.

I also tried to perform the same thing in another way with ifelse:
for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
corr.iris - ifelse(i==1  j==2, cor.temp, c(corr.iris, cor.temp))
}
}

This is not working. Seems the value of c(corr.iris, cor.temp) has not
been assigned to corr.iris, even  when the (i==1  j==2) is not satisfied.

what's the problem here?

thanks,

karena
-- 
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-tp2281576p2281576.html
Sent from the R help mailing list archive at Nabble.com.

__
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] ifelse statement

2010-07-07 Thread Duncan Murdoch

On 07/07/2010 5:58 PM, karena wrote:

Hi, I am a newbie of R, and playing with the ifelse statement.

I have the following codes:
## first,

for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
if(i==1  j==2) corr.iris - cor.temp
else corr.iris - c(corr.iris, cor.temp)
}
}

this code is working fine.

I also tried to perform the same thing in another way with ifelse:
for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
corr.iris - ifelse(i==1  j==2, cor.temp, c(corr.iris, cor.temp))
}
}

This is not working. Seems the value of c(corr.iris, cor.temp) has not
been assigned to corr.iris, even  when the (i==1  j==2) is not satisfied.

what's the problem here?



See ?ifelse.  It computes something the same shape as the test object.  
In your case the test is the result of


i==1  j==2

and is a scalar that is either TRUE or FALSE, so the result of ifelse() 
will be a scalar too.


To do what you want in one line, you can use

corr.iris - if (i==1  j==2) cor.temp else c(corr.iris, cor.temp)

but to most people this looks unnatural, and your original code is what I'd recommend using.  In this 
case it makes no difference whether you use  or  in the test, but in other cases only 
 makes sense with if, and only  makes sense with ifelse().

Duncan Murdoch

__
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] ifelse statement

2010-07-07 Thread Gabor Grothendieck
On Wed, Jul 7, 2010 at 7:22 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote:
 On 07/07/2010 5:58 PM, karena wrote:

 Hi, I am a newbie of R, and playing with the ifelse statement.

 I have the following codes:
 ## first,

 for(i in 1:3) {
 for(j in 2:4) {
 cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
 if(i==1  j==2) corr.iris - cor.temp
 else corr.iris - c(corr.iris, cor.temp)
 }
 }

 this code is working fine.

 I also tried to perform the same thing in another way with ifelse:
 for(i in 1:3) {
 for(j in 2:4) {
 cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
 corr.iris - ifelse(i==1  j==2, cor.temp, c(corr.iris, cor.temp))
 }
 }

 This is not working. Seems the value of c(corr.iris, cor.temp) has not
 been assigned to corr.iris, even  when the (i==1  j==2) is not satisfied.

 what's the problem here?


 See ?ifelse.  It computes something the same shape as the test object.  In
 your case the test is the result of

 i==1  j==2

 and is a scalar that is either TRUE or FALSE, so the result of ifelse() will
 be a scalar too.

 To do what you want in one line, you can use

 corr.iris - if (i==1  j==2) cor.temp else c(corr.iris, cor.temp)

 but to most people this looks unnatural, and your original code is what I'd
 recommend using.  In this case it makes no difference whether you use  or
  in the test, but in other cases only  makes sense with if, and only 
 makes sense with ifelse().

Just to quibble I find the

   corr.iris - if ...

construct easier to understand than

   if (...) corr.iris - ... else corr.iris - ...

because in the first case you immediately see that the purpose of the
construct is to set corr.iris whereas setting it separately in each
leg requires that you must examine more code, i.e. both legs, to make
such a determination adding to the mental load.

__
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] ifelse statement

2010-07-07 Thread Duncan Murdoch

On 07/07/2010 7:32 PM, Gabor Grothendieck wrote:

On Wed, Jul 7, 2010 at 7:22 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote:
  

On 07/07/2010 5:58 PM, karena wrote:


Hi, I am a newbie of R, and playing with the ifelse statement.

I have the following codes:
## first,

for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
if(i==1  j==2) corr.iris - cor.temp
else corr.iris - c(corr.iris, cor.temp)
}
}

this code is working fine.

I also tried to perform the same thing in another way with ifelse:
for(i in 1:3) {
for(j in 2:4) {
cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
corr.iris - ifelse(i==1  j==2, cor.temp, c(corr.iris, cor.temp))
}
}

This is not working. Seems the value of c(corr.iris, cor.temp) has not
been assigned to corr.iris, even  when the (i==1  j==2) is not satisfied.

what's the problem here?
  

See ?ifelse.  It computes something the same shape as the test object.  In
your case the test is the result of

i==1  j==2

and is a scalar that is either TRUE or FALSE, so the result of ifelse() will
be a scalar too.

To do what you want in one line, you can use

corr.iris - if (i==1  j==2) cor.temp else c(corr.iris, cor.temp)

but to most people this looks unnatural, and your original code is what I'd
recommend using.  In this case it makes no difference whether you use  or
 in the test, but in other cases only  makes sense with if, and only 
makes sense with ifelse().



Just to quibble I find the

   corr.iris - if ...

construct easier to understand than

   if (...) corr.iris - ... else corr.iris - ...

because in the first case you immediately see that the purpose of the
construct is to set corr.iris whereas setting it separately in each
leg requires that you must examine more code, i.e. both legs, to make
such a determination adding to the mental load.
  


Well, I did say most people, not all people.  The reason I think 
most people prefer the separate statement is that they don't realize 
that if (test) value1 else value2 is just a different way to write the 
function call `if`(test, value1, value2), they think of it as a 
flow-of-control statement, as it is in languages like C.


Duncan Murdoch

__
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] ifelse statement

2010-07-07 Thread Godfrey van der Linden

On 2010-07-08, at 10:33 , Duncan Murdoch wrote:

 On 07/07/2010 7:32 PM, Gabor Grothendieck wrote:
 On Wed, Jul 7, 2010 at 7:22 PM, Duncan Murdoch murdoch.dun...@gmail.com 
 wrote:
  
 On 07/07/2010 5:58 PM, karena wrote:

 Hi, I am a newbie of R, and playing with the ifelse statement.
 
 I have the following codes:
 ## first,
 
 for(i in 1:3) {
 for(j in 2:4) {
 cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
 if(i==1  j==2) corr.iris - cor.temp
 else corr.iris - c(corr.iris, cor.temp)
 }
 }
 
 this code is working fine.
 
 I also tried to perform the same thing in another way with ifelse:
 for(i in 1:3) {
 for(j in 2:4) {
 cor.temp - cor(iris.allnum[,i], iris.allnum[,j])
 corr.iris - ifelse(i==1  j==2, cor.temp, c(corr.iris, cor.temp))
 }
 }
 
 This is not working. Seems the value of c(corr.iris, cor.temp) has not
 been assigned to corr.iris, even  when the (i==1  j==2) is not satisfied.
 
 what's the problem here?
  
 See ?ifelse.  It computes something the same shape as the test object.  In
 your case the test is the result of
 
 i==1  j==2
 
 and is a scalar that is either TRUE or FALSE, so the result of ifelse() will
 be a scalar too.
 
 To do what you want in one line, you can use
 
 corr.iris - if (i==1  j==2) cor.temp else c(corr.iris, cor.temp)
 
 but to most people this looks unnatural, and your original code is what I'd
 recommend using.  In this case it makes no difference whether you use  or
  in the test, but in other cases only  makes sense with if, and only 
 makes sense with ifelse().

 
 Just to quibble I find the
 
   corr.iris - if ...
 
 construct easier to understand than
 
   if (...) corr.iris - ... else corr.iris - ...
 
 because in the first case you immediately see that the purpose of the
 construct is to set corr.iris whereas setting it separately in each
 leg requires that you must examine more code, i.e. both legs, to make
 such a determination adding to the mental load.
  
 
 Well, I did say most people, not all people.  The reason I think most 
 people prefer the separate statement is that they don't realize that if 
 (test) value1 else value2 is just a different way to write the function call 
 `if`(test, value1, value2), they think of it as a flow-of-control 
 statement, as it is in languages like C.
 
 Duncan Murdoch

I'm a newbie too, but I'm sort of curious about the functional programming 
approach

How does the following code fragment work for you

ind - matrix(c(rep(1:3, each=3), rep(2:4, 3)), nrows=3*3, ncols=2)
result = ifelse(inds[,1] == 1  inds[,2] == 2, dotrue, dofalse)

This code is more concise.

Is it more efficient? I assume a lot of this code is done in C rather than the 
interpreted for loops around if()else statements.  Is it more memory intensive?

Godfrey

__
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] ifelse statement

2010-07-07 Thread karena

that makes sense. thank you, guys!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/ifelse-statement-tp2281576p2281706.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


  1   2   >