[R] assigning dataframes in an ifelse statement

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Mar 17 06:20:52 CET 2014


Solution is to not use ifelse. Use if...else...
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On March 16, 2014 9:58:25 PM PDT, BEUTEL Terry S <Terry.Beutel at daff.qld.gov.au> wrote:
>Hi everyone.
>
>I have three dataframes (A, B and C). B and C are subsets of the rows
>of A, for example...
>
>> A<-as.data.frame(cbind(1:5,21:25))
>> A
>  V1 V2
>1  1 21
>2  2 22
>3  3 23
>4  4 24
>5  5 25
>
>> B<-A[1:4,]
>> B
>  V1 V2
>1  1 21
>2  2 22
>3  3 23
>4  4 24
>
>
>> C<-A[3:5,]
>> C
>  V1 V2
>3  3 23
>4  4 24
>5  5 25
>
>
>
>I also have a single value called x
>> x<-2
>
>Basically I want to evaluate x and if it equals 2, then replace A with
>B, and otherwise replace A with C. However, when I attempt this, the
>new version of A is only a subset of the desired dataframe. For
>example.
>
>>
>> A<-ifelse(x==2,B,C)
>> A
>[[1]]
>[1] 1 2 3 4
>
>I'm sure this is simple, but haven't found a solution and would
>appreciate a tip.
>
>Thanks
>
>Terry Beutel
>
>
>------------------------------
>The information in this email together with any attachments is intended
>only for the person or entity to which it is addressed and may contain
>confidential and/or privileged material. There is no waiver of any
>confidentiality/privilege by your inadvertent receipt of this material.
>
>Any form of review, disclosure, modification, distribution and/or
>publication of this email message is prohibited, unless as a necessary
>part of Departmental business.
>If you have received this message in error, you are asked to inform the
>sender as quickly as possible and delete this message and any copies of
>this message from your computer and/or your computer system network.
>------------------------------
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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.




More information about the R-help mailing list