2015-06-18 212 views
0

我是R編程的初學者(我剛剛完成Coursera課程),並且無法創建此嵌套循環。R:嵌套排序

我有結構化的這樣一個CSV(實際上有108列):

Type  Status Campaign Name Group  Budget Budget Type Bids 
Campaign Active Burritos     500  Daily 
Campaign Active Tacos      400  Daily 
Group Active Burritos Bean Burritos       0.5 
Group Active Burritos Beef Burritos       0.5 
Group Paused Burritos Chicken Burritos      0.5 
Group Active Tacos  Beef Tacos       0.5 
Group Active Tacos  Chicken Tacos       0.5 
Group Paused Tacos  Fish Tacos       0.5 

我想重新安排廣告系列名稱,則表中去除暫停:

Type  Status Campaign Name Group  Budget Budget Type  Bids 
Campaign Active Burritos     500  Daily 
Group Active Burritos Bean Burritos       0.5 
Group Active Burritos Beef Burritos       0.5 
Campaign Active Tacos      400  Daily 
Group Active Tacos  Beef Tacos       0.5 
Group Active Tacos  Chicken Tacos       0.5 

我要使用一系列的For循環,但我一直在遇到錯誤。我很確定這個rbind有錯誤。另外,當我創建temp.ds和temp.group.ds時,我認爲存在錯誤。也可能是循環中的錯誤。

下面是我的代碼:R中

ds <- do.call(rbind, lapply(list.files(path=directory, full.names=TRUE), read.table, header=TRUE, sep="\t", fileEncoding="UTF-16LE", fill = TRUE, quote = "")) 

valid.campaign <- ds[ which(ds$Status == "Active" & ds$Type == "Campaign"), ] 

new.ds <- NULL 

for(campaign in valid.campaign$Type) { 
    temp.ds <- valid.campaign[,campaign] 
    valid.group <- ds[ which(ds$Status == "Active" & ds$Type == "Group"), ] 

    for (group in valid.group$Type) { 
    temp.group.ds <- valid.group[,group] 
    temp.ds <-rbind(temp.ds, temp.group.ds) 
    rm(temp.group.ds) 
    } 

    if (exists("new.ds")) new.ds <- rbind(new.ds,temp.ds) 
    else new.ds <- temp.ds 
    rm(temp.ds) 
    } 
new.ds 
} 
+0

由於R是一種解釋語言,你可以執行的代碼逐行。這應該使您能夠找到引發錯誤的行。一邊的說明:你應該嘗試在Stackoverflow上發佈可複製的代碼。 – cryo111

+1

試試'library(dplyr); ds%>%arrange(CampaignName)%>%filter(Status!=「Paused」)' – Khashaa

+0

您可以輸入您的數據嗎? – Hav0k

回答

0

的dplyr和magrittr包都是優秀的處理這些各種各樣的問題。具體而言,在dplyr的安排功能可以安排行,並在dplyr過濾功能允許您刪除行:

ds %<>% arrange(CampaignName, Group) %>% filter(Status != 'Paused') 
0

在基地R我會用下面的代碼兩行。第一個是排序,第二個是子集。當然有辦法把它包在oneliner,但我認爲這是更具可讀性這樣的:

ds = ds[order(ds$Campaign_Name, ds$Group),] 
ds = ds[which(ds$Status != "Paused"),] 

給我們:

 Type Status Campaign_Name   Group Budget Budget_Type Bids 
1 Campaign Active  Burritos     500 Daily  NA 
3 Group Active  Burritos Bean Burritos  NA    0.5 
4 Group Active  Burritos Beef Burritos  NA    0.5 
2 Campaign Active   Tacos     400 Daily  NA 
6 Group Active   Tacos Beef Tacos  NA    0.5 
7 Group Active   Tacos Chicken Tacos  NA    0.5