我有以下的數據幀替代與多個條件嵌套ifelse語句的一長串
structure(list(FY = c("2015-2016", "2015-2016", "2015-2016",
"2015-2016"), YEARMN = structure(c(2015.25, 2015.25, 2015.25,
2015.25), class = "yearmon"), BRAND = c("3M CAR CARE", "CAR CARE 3M",
"CAR CARE 3M", "CAR CARE 3M"), variable = structure(c(1L,
2L, 3L, 4L), .Label = c("IstWEEKRent", "IIndWEEKRent", "IIIrdWEEKRent",
"IVthWEEKRent", "mymonth"), class = "factor"), value = c("0",
"17500", "85000", "212500"), mymonth = c("Apr", "Apr", "Apr",
"Apr")), .Names = c("FY", "YEARMN", "BRAND", "variable", "value",
"mymonth"), row.names = c(NA, 4L), class = "data.frame")
實際的數據幀是這樣的:
FY YEARMN BRAND variable value mymonth
1 2015-2016 Apr 2015 3M CAR CARE IstWEEKRent 0 Apr
2 2015-2016 Apr 2015 CAR CARE 3M IIndWEEKRent 17500 Apr
3 2015-2016 Apr 2015 CAR CARE 3M IIIrdWEEKRent 85000 Apr
4 2015-2016 Apr 2015 CAR CARE 3M IVthWEEKRent 212500 Apr
我的月柱,從四月有個月到Mar ...每個月在我的數據集中有4個星期,這是在列變量中給出的。我想創建爲FY四月每週數日 - 3月,開始從1到48,我想給週數1相匹配
variable == "IstWeekRent" & mymonth == "Apr"
我用ifelse函數來完成這件事的情況...其工作正常......但是當我有同樣的到我閃亮的應用我收到以下錯誤:
Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) :
contextstack overflow at line 2870
我現在ifelse條件語句如下所示:
trndR$weeks <- ifelse(trndR$mymonth == "Apr" & trndR$variable == "IstWEEKRent", 1,
ifelse(trndR$mymonth == "Apr" & trndR$variable == "IIndWEEKRent", 2,
ifelse(trndR$mymonth == "Apr" & trndR$variable == "IIIrdWEEKRent", 3,
ifelse(trndR$mymonth == "Apr" & trndR$variable == "IVthWEEKRent", 4,
ifelse(trndR$mymonth == "May" & trndR$variable == "IstWEEKRent", 5,
ifelse(trndR$mymonth == "May" & trndR$variable == "IIndWEEKRent", 6,
trndR
是我的df的名稱,並且條件延伸到48.
我發現我可以只有最多50個嵌套ifelse條件...但不太確定如何糾正這個問題。我讀了關於應用函數,但不知道如何在這種情況下使用它。
該數據是否有排序?您顯示的數據看起來像是每星期有一行一致,在這種情況下,行號可能足夠用於「周」列。 – rosscova