2015-05-15 122 views
-2

取樣輸入數據(存儲在CSV在工作目錄):基於R嵌套IfElse語句來比較兩個變量字符串

EMPLID,From_DeptCode,FromDept,To_DeptCode,To_Dept,TransactionTypeCode,TransactionType,EffectiveDate,ChangeType 
0239583290,21,Sales,43,CustomerService,10,Promotion,12/12/2012 
1230495829,21,Sales,21,Sales,10,Promotion,9/1/2013 
4059503918,93,Operations,93,Operations,13,Demotion,11/18/2014 
3040593021,19,Headquarters,23,International,11,Reorg,12/13/2011 
7029406920,15,Marketing,84,Development,19,Reassignment,1/5/2010 
2039052819,19,Headquarters,19,Headquarters,10,Promotion,4/15/2015 

代碼:

Transfers <- read.csv(file="Transfers.csv", head=TRUE, sep=",",colClasses=c(NA,NA,NA,NA,NA,NA,NA,"Date",NA)) 

Transfers$ChangeType <- ifelse(Transfers$From_DeptCode == Transfers$To_DeptCode, "No Change", ifelse(Transfers$TransactionType == "Reorg", "Reorg", "Transfer")) 

Transfers2 <- subset(Transfers, ChangeType != "No Change") 

print(Transfers2) 

預期輸出:

EMPLID,From_DeptCode,FromDept,To_DeptCode,To_Dept,TransactionTypeCode,TransactionType,EffectiveDate,ChangeType 
0239583290,21,Sales,43,CustomerService,10,Promotion,12/12/2012,Transfer 
3040593021,19,Headquarters,23,International,11,Reorg,12/13/2011,Reorg 
7029406920,15,Marketing,84,Development,19,Reassignment,1/5/2010,Transfer 

實際:

> print(Transfers2) 
Error in print(Transfers2) : object 'Transfers2' not found 

不知道爲什麼會發生這種情況。

+0

無法重現。我將你的示例輸入數據複製到一個'Transfers.csv'文件中,執行你的行和一切工作。 – nicola

+0

如果您收到消息「找不到轉移2」,則表示在前面的一行代碼中出現錯誤。據我可以告訴你的代碼讀取CSV文件不符合你發佈的樣本數據。 – MrFlick

+0

@MrFlick我的代碼中的什麼讓你認爲它看起來與數據不符? – bw1984

回答

-1

所以我發現我的問題..

爲了讓我的代碼工作,我不得不把我的光標在每個代碼塊的行的末尾,爲了單獨運行它們。

我跳到腳本的底部,將光標放在代碼的最後一行下面,運行它並獲取錯誤。

爲什麼我需要用塊中的光標一次一個地運行每個代碼塊?

+0

這不是答案。 – user227710