我是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
}
由於R是一種解釋語言,你可以執行的代碼逐行。這應該使您能夠找到引發錯誤的行。一邊的說明:你應該嘗試在Stackoverflow上發佈可複製的代碼。 – cryo111
試試'library(dplyr); ds%>%arrange(CampaignName)%>%filter(Status!=「Paused」)' – Khashaa
您可以輸入您的數據嗎? – Hav0k