Dwaipayan Dasgupta
2012-Apr-18 12:36 UTC
[R] Help with creating conditional categorical variables in R
Hi, I am stuck with creating a conditional categorical variable in R If my dataframe data_2 has 3 variables A,B,C I want to create variable D which would be something like : data_2$D <- ifelse(data_2$A < data_2$B & promotion_ind =="N",1,0), this throws up an error "In Ops.factor(data_2$A,data_2$B) : < not meaningful for factors I have tried converting the dataframe to a matrix using data.matrix(data_2, rownames.force = NA) but that did not help, Can anybody tell me how to go about doing this? Thanks in advance, Doy American Express made the following annotations on Wed Apr 18 2012 05:37:16 ****************************************************************************** "This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." American Express a ajouté le commentaire suivant le Wed Apr 18 2012 05:37:16 Ce courrier et toute pièce jointe qu'il contient sont réservés au seul destinataire indiqué et peuvent renfermer des renseignements confidentiels et privilégiés. Si vous n'êtes pas le destinataire prévu, toute divulgation, duplication, utilisation ou distribution du courrier ou de toute pièce jointe est interdite. Si vous avez reçu cette communication par erreur, veuillez nous en aviser par courrier et détruire immédiatement le courrier et les pièces jointes. Merci. ****************************************************************************** ------------------------------------------------------------------------------- [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Apr-18 15:35 UTC
[R] Help with creating conditional categorical variables in R
Your problem is that columns A & B contain something that can't be ordered. (Likely "factor" (=categorical) data like Male / Female rather than numeric like 10 and 5) Use str(data_2) to see what classes your data are -- they sometimes get converted in unexpected ways if you aren't careful in setting up your data.frame. If this doesn't clear things up, please type dput(head(data_2, 20)) at the console and copy and paste the result into a follow up email. Don't do the data.matrix thing without understanding the current class of your data -- you'll get strange conversions. Hope this helps, Michael On Wed, Apr 18, 2012 at 8:36 AM, Dwaipayan Dasgupta <Dwaipayan.Dasgupta at aexp.com> wrote:> Hi, > I am stuck with creating a conditional categorical variable in R > If my dataframe ?data_2 ?has 3 variables A,B,C ? ?I want to create variable D which would be something like : > > data_2$D <- ifelse(data_2$A < data_2$B & promotion_ind =="N",1,0), > this throws up an error "In Ops.factor(data_2$A,data_2$B) : > ?< not meaningful for factors > > I have tried converting the dataframe to a matrix using > data.matrix(data_2, rownames.force = NA) but that did not help, > > Can anybody tell me how to go about doing this? > > Thanks in advance, > Doy > > American Express made the following annotations on Wed Apr 18 2012 05:37:16 > > ****************************************************************************** > > "This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." > > American Express a ajout? le commentaire suivant le Wed Apr 18 2012 05:37:16 > > Ce courrier et toute pi?ce jointe qu'il contient sont r?serv?s au seul destinataire indiqu? et peuvent renfermer des renseignements confidentiels et privil?gi?s. Si vous n'?tes pas le destinataire pr?vu, toute divulgation, duplication, utilisation ou distribution du courrier ou de toute pi?ce jointe est interdite. Si vous avez re?u cette communication par erreur, veuillez nous en aviser par courrier et d?truire imm?diatement le courrier et les pi?ces jointes. Merci. > > ****************************************************************************** > ------------------------------------------------------------------------------- > > > ? ? ? ?[[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. >