How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Pete Your Own Question
Pete
Pete, Computer Engineer
Category: Programming
Satisfied Customers: 16697
Experience:  Over 16 years of experience in computer programming.
30970729
Type Your Programming Question Here...
Pete is online now
A new question is answered every 9 seconds

I'm using R and would like to compare two columns and write

Customer Question

I'm using R and would like to compare two columns and write out the results to a new csv file.
Sample of (mocked up) data:
EMPLID,From_DeptCode,FromDept,To_DeptCode,To_Dept,TransactionTypeCode,TransactionType,EffectiveDate,ChangeType (###) ###-####21,Sales,43,CustomerService,10,Promotion,12/12/2012 (###) ###-####21,Sales,21,Sales,10,Promotion,9/1/2013 (###) ###-####93,Operations,93,Operations,10,Demotion,11/18/2014 (###) ###-####19,Headquarters,23,International,11,Reorg,12/13/2011 (###) ###-####15,Marketing,84,Development,19,Reassignment,01/05/2010 (###) ###-####19,Headquarters,19,Headquarters,10,Promotion,4/15/2015
The logic I want to use is:
If From_DeptCode = To_DeptCode
then Pass/Do Nothing
ElseIf From_DeptCode != To_DeptCode AND TransactionType = "Reorg"
then ChangeType="Reorg"
Else ChangeType="Transfer"
So my output would look like:
EMPLID,From_DeptCode,FromDept,To_DeptCode,To_Dept,TransactionTypeCode,TransactionType,EffectiveDate,ChangeType (###) ###-####21,Sales,43,CustomerService,10,Promotion,12/12/2012,Transfer (###) ###-####19,Headquarters,23,International,11,Reorg,12/13/2011,Reorg (###) ###-####15,Marketing,84,Development,19,Reassignment,01/05/2010,Transfer
Here's what I know so far:
transfers <- read.csv(file="Transfers.csv", head=TRUE,
sep=",",colClasses=c(NA,NA,NA,NA,NA,NA,NA,"Date",NA))
at this point, I would, I assume, implement my logic:
If From_DeptCode = To_DeptCode
then skip
ElseIf From_DeptCode != To_DeptCode AND TransactionType = "Reorg"
then ChangeType="Reorg"
Else ChangeType="Transfer"
I assume that here I'd write out my new csv
write.csv(transfers, file = "transfersprocessed.csv", row.names = FALSE)
Any advice on getting the rest of the way there?
Submitted: 1 year ago.
Category: Programming
Expert:  Pete replied 1 year ago.
Hello,
I see you have been waiting some time for an answer.
Let me know if the following code is along the right lines:
http://stackoverflow.com/questions/7653141/comparing-two-columns-in-a-data-frame-across-many-rows

Related Programming Questions