I have created this data frame to help illustrate my problem. id<-c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5)) x<-rep((seq(1:5)),5) y<-c(0, 0.1, 0.5, 0.4, 0.2, 0, 0.1, 0.5, 0.4, 0.12, 0, 0.1, 0.5, 0.55, 0.2, 0, 0.1, 0.5, 0.3, 0.2, 0, 0.1, 0.6, 0.4, 0.1) d1<-cbind(id,x,y) I would like to delete all rows where id=4, however, when I tried the command d2=d1[-c(id==4),] and looked at d2 no data was removed. Thanks in advance, I appreciate any help. [[alternative HTML version deleted]]
Rowe, Brian Lee Yung (Portfolio Analytics)
2009-Apr-02 20:37 UTC
[R] Deleting rows based on identity variable
Is this what you want:> d1[which(id != 4),]id x y [1,] 1 1 0.00 [2,] 1 2 0.10 [3,] 1 3 0.50 [4,] 1 4 0.40 [5,] 1 5 0.20 [6,] 2 1 0.00 [7,] 2 2 0.10 [8,] 2 3 0.50 [9,] 2 4 0.40 [10,] 2 5 0.12 [11,] 3 1 0.00 [12,] 3 2 0.10 [13,] 3 3 0.50 [14,] 3 4 0.55 [15,] 3 5 0.20 [16,] 5 1 0.00 [17,] 5 2 0.10 [18,] 5 3 0.60 [19,] 5 4 0.40 [20,] 5 5 0.10 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of gina patel Sent: Thursday, April 02, 2009 4:30 PM To: R-help at r-project.org Subject: [R] Deleting rows based on identity variable I have created this data frame to help illustrate my problem. id<-c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5)) x<-rep((seq(1:5)),5) y<-c(0, 0.1, 0.5, 0.4, 0.2, 0, 0.1, 0.5, 0.4, 0.12, 0, 0.1, 0.5, 0.55, 0.2, 0, 0.1, 0.5, 0.3, 0.2, 0, 0.1, 0.6, 0.4, 0.1) d1<-cbind(id,x,y) I would like to delete all rows where id=4, however, when I tried the command d2=d1[-c(id==4),] and looked at d2 no data was removed. Thanks in advance, I appreciate any help. [[alternative HTML version deleted]] -------------------------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. --------------------------------------------------------------------------
On Thu, Apr 2, 2009 at 3:37 PM, Rowe, Brian Lee Yung (Portfolio Analytics) <B_Rowe at ml.com> wrote:> Is this what you want: >> d1[which(id != 4),]Or just d1[id != 4, ] Hadley -- http://had.co.nz/
I have another question, if I now want to remove multiple id's e.g. id=1 or 4 is there a simple OR command I can use? I tried d2<-(d1[id != 1 | 4, ]) however this does not delete anything PS d2<-(d1[id != 4, ]) worked to remove id=4 Thanks Gina --- On Thu, 4/2/09, hadley wickham <h.wickham@gmail.com> wrote: From: hadley wickham <h.wickham@gmail.com> Subject: Re: [R] Deleting rows based on identity variable To: "Rowe, Brian Lee Yung (Portfolio Analytics)" <B_Rowe@ml.com> Cc: "gina patel" <ginapatel1981@yahoo.com>, R-help@r-project.org Date: Thursday, April 2, 2009, 4:48 PM On Thu, Apr 2, 2009 at 3:37 PM, Rowe, Brian Lee Yung (Portfolio Analytics) <B_Rowe@ml.com> wrote:> Is this what you want: >> d1[which(id != 4),]Or just d1[id != 4, ] Hadley -- http://had.co.nz/ [[alternative HTML version deleted]]
On 3/04/2009, at 9:30 AM, gina patel wrote:> I have created this data frame to help illustrate my problem. > > id<-c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5)) > x<-rep((seq(1:5)),5) > y<-c(0, 0.1, 0.5, 0.4, 0.2, 0, 0.1, 0.5, 0.4, 0.12, 0, 0.1, 0.5, > 0.55, 0.2, 0, 0.1, 0.5, 0.3, 0.2, 0, 0.1, 0.6, 0.4, 0.1) > d1<-cbind(id,x,y) > > I would like to delete all rows where id=4, however, when I tried > the command > > d2=d1[-c(id==4),] > > and looked at d2 no data was removed. > > Thanks in advance, I appreciate any help.Others have told you how to do what you want. It would be useful for you to understand why what you ***did*** didn't work. Note that ``id==4'' yields a *logical* vector with entries TRUE and FALSE. When an arithmetic operator is applied, as in -(id==4) [NOTE: THE ``c ()'' wrapper is TOTALLY UNNECESSARY HERE.] the vector is coerced to a numeric vector of -1's (for TRUE) and 0's (for FALSE). Thus you are asking d2 to be d1 [i,] where i is a vector of -1's and 0's. The -1's say to throw away the ``1- th'' (first) row of d1; the 0's say to pick out the 0-th row of d1 (and there is no 0-th row so this doesn't do anything). Thus you are in effect asking for d1 [2:25,]. And that's what you get. Look carefully --- it is not correct to say that when you looked at d2 *no* data were removed; d2 is equal to d1 with its first row removed. I.e. your operation didn't remove what you wanted, but it removed *something*. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}