Re: [R] question for if else

2006-12-08 Thread jim holtman
Hopefully it is a dataframe or else you matrix will be converted to
character; forgot that when I sent it.

On 12/8/06, jim holtman <[EMAIL PROTECTED]> wrote:
>
> If p1982 is a data.frame:
>
> p1982$Y <- ifelse(p1982$sc<90, 'inward', 'outward')
>
> If it is a matrix
>
> p1982 <- cbind(p1982, Y=ifelse(p1982[,'sc']<90, 'inward', 'outward'))
>
>
>  On 12/8/06, Aimin Yan <[EMAIL PROTECTED]> wrote:
> >
> > I have a data set like this
> > I want to assign "outward" to Y if sc <90 and assign "inward" to Y if
> > sc>=90.
> > then cbind(p1982,Y) to get like these
> >
> > p aa as ms cur sc Y
> > 1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410   inward
> > 2 154l_aa THR  15.86  28.32  0.2563560 103.67100inward
> > 3 154l_aa ASP  65.13  59.16   0.0312137   7.27311 outward
> > 4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930outward
> > 5 154l_aa TYR  28.87  31.75  0.0526457  96.11660 inward
> > 6 154l_aa ASN  31.14  31.09  0.0632711  55.65980outward
> >
> > does anyone know how to these?
> >
> > Aimin
> >
> > > head(p1982)
> > p  aa as mscursc
> > 1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410
> > 2 154l_aa THR  15.86  28.32  0.2563560 103.67100
> > 3 154l_aa ASP  65.13  59.16  0.0312137   7.27311
> > 4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930
> > 5 154l_aa TYR  28.87  31.75  0.0526457  96.11660
> > 6 154l_aa ASN  31.14  31.09  0.0632711  55.65980
> >
> > __
> > R-help@stat.math.ethz.ch 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 you are trying to solve?




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

What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] question for if else

2006-12-08 Thread jim holtman
If p1982 is a data.frame:

p1982$Y <- ifelse(p1982$sc<90, 'inward', 'outward')

If it is a matrix

p1982 <- cbind(p1982, Y=ifelse(p1982[,'sc']<90, 'inward', 'outward'))


On 12/8/06, Aimin Yan <[EMAIL PROTECTED]> wrote:
>
> I have a data set like this
> I want to assign "outward" to Y if sc <90 and assign "inward" to Y if
> sc>=90.
> then cbind(p1982,Y) to get like these
>
> p aa as ms cur sc Y
> 1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410   inward
> 2 154l_aa THR  15.86  28.32  0.2563560 103.67100inward
> 3 154l_aa ASP  65.13  59.16  0.0312137   7.27311 outward
> 4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930outward
> 5 154l_aa TYR  28.87  31.75  0.0526457  96.11660 inward
> 6 154l_aa ASN  31.14  31.09  0.0632711  55.65980outward
>
> does anyone know how to these?
>
> Aimin
>
> > head(p1982)
> p  aa as mscursc
> 1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410
> 2 154l_aa THR  15.86  28.32  0.2563560 103.67100
> 3 154l_aa ASP  65.13  59.16  0.0312137   7.27311
> 4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930
> 5 154l_aa TYR  28.87  31.75  0.0526457  96.11660
> 6 154l_aa ASN  31.14  31.09  0.0632711  55.65980
>
> __
> R-help@stat.math.ethz.ch 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 you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] question for if else

2006-12-08 Thread Aimin Yan
I have a data set like this
I want to assign "outward" to Y if sc <90 and assign "inward" to Y if sc>=90.
then cbind(p1982,Y) to get like these

p aa as ms cur sc Y
1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410   inward
2 154l_aa THR  15.86  28.32  0.2563560 103.67100inward
3 154l_aa ASP  65.13  59.16  0.0312137   7.27311 outward
4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930outward
5 154l_aa TYR  28.87  31.75  0.0526457  96.11660 inward
6 154l_aa ASN  31.14  31.09  0.0632711  55.65980outward

does anyone know how to these?

Aimin

 > head(p1982)
 p  aa as mscursc
1 154l_aa ARG 152.04 108.83 -0.1020140  92.10410
2 154l_aa THR  15.86  28.32  0.2563560 103.67100
3 154l_aa ASP  65.13  59.16  0.0312137   7.27311
4 154l_aa CYS  57.20  49.85 -0.0549589  72.97930
5 154l_aa TYR  28.87  31.75  0.0526457  96.11660
6 154l_aa ASN  31.14  31.09  0.0632711  55.65980

__
R-help@stat.math.ethz.ch 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] question for IF ELSE usage

2005-07-12 Thread ecoinfo
Marc,
 I see. Thanks. 
 Xiaohua
 On 7/12/05, Marc Schwartz (via MN) <[EMAIL PROTECTED]> wrote: 
> 
> On Tue, 2005-07-12 at 16:22 +0200, ecoinfo wrote:
> > Hi R users,
> > Maybe the question is too simple.
> > In a IF ... ELSE ... statement "if(cond) cons.expr else alt.expr", IF 
> and
> > ELSE should be at the same line?
> > For example,
> > if (x1==12)
> > {
> > y1 <- 5
> > }else
> > {
> > y1 <- 3
> > }
> > is right, while
> > if (x1==12)
> > {
> > y1 <- 5
> > }
> > else # Error: syntax error
> > {
> > y1 <- 3
> > }
> > is wrong?
> > Thanks
> 
> Note the following from the Details section of ?"if"
> 
> "Note that it is a common mistake to forget to put braces ({ .. })
> around your statements, e.g., after if(..) or for(). In particular,
> you should not have a newline between } and else to avoid a syntax error
> in entering a if ... else construct at the keyboard or via source. For
> that reason, one (somewhat extreme) attitude of defensive programming is
> to always use braces, e.g., for if clauses."
> 
> 
> One other approach is the following:
> 
> if (x1 == 12)
> {
> y1 <- 5
> } else {
> y1 <- 3
> }
> 
> Note the presence of both braces on the 'else' line.
> 
> HTH,
> 
> Marc Schwartz
> 
> 
> 


--

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] question for IF ELSE usage

2005-07-12 Thread Marc Schwartz (via MN)
On Tue, 2005-07-12 at 16:22 +0200, ecoinfo wrote:
> Hi R users,
>  Maybe the question is too simple.
>  In a IF ... ELSE ... statement "if(cond) cons.expr else alt.expr", IF and 
> ELSE should be at the same line? 
> For example,
>  if (x1==12)
> {
> y1 <- 5 
> }else
> {
> y1 <- 3
> }
>  is right, while
>  if (x1==12)
> {
> y1 <- 5 
> }
> else # Error: syntax error
> {
> y1 <- 3
> }
>  is wrong? 
>  Thanks

Note the following from the Details section of ?"if"

"Note that it is a common mistake to forget to put braces ({ .. })
around your statements, e.g., after if(..) or for(). In particular,
you should not have a newline between } and else to avoid a syntax error
in entering a if ... else construct at the keyboard or via source. For
that reason, one (somewhat extreme) attitude of defensive programming is
to always use braces, e.g., for if clauses."


One other approach is the following:

if (x1 == 12) 
{
  y1 <- 5 
} else {
  y1 <- 3
}

Note the presence of both braces on the 'else' line.

HTH,

Marc Schwartz

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] question for IF ELSE usage

2005-07-12 Thread ecoinfo
Hi R users,
 Maybe the question is too simple.
 In a IF ... ELSE ... statement "if(cond) cons.expr else alt.expr", IF and 
ELSE should be at the same line? 
For example,
 if (x1==12)
{
y1 <- 5 
}else
{
y1 <- 3
}
 is right, while
 if (x1==12)
{
y1 <- 5 
}
else # Error: syntax error
{
y1 <- 3
}
 is wrong? 
 Thanks

-- 
Xiaohua Dai

Centre for Systems Research, Durban Institute of Technology
P.O.Box 953, Durban 4000, South Africa
Tel: +27-31-2042737(O) Fax: +27-31-2042736(O)
Mobile: +27-723682954

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html