像下面關於聚合函數的源代碼,我不明白爲什麼我們必須在這裏使用list函數()。而不是我想用一個需要分組的列來替換它。我不知道爲什麼我們使用相同的數據集,例如'train [Sales!= 0]'兩次?如果我使用其他數據集作爲第二個數據集參數,該怎麼辦?我認爲這會使改變成爲相當高的可能性錯誤。聚合函數不直觀
aggregate(train[Sales != 0]$Sales,
by = list(train[Sales != 0]$Store), mean)
也許有人會說這是錯誤的用例。但我也在R中看到這個源代碼
## Compute the averages for the variables in 'state.x77', grouped
## according to the region (Northeast, South, North Central, West) that
## each state belongs to.
aggregate(state.x77, list(Region = state.region), mean)
感謝您閱讀我的問題。
我認爲你的代碼可能是錯誤的。如果'train'是一個矩陣或data.frame這個命令'train [Sales!= 0]'給你一個錯誤('undefined columns selected')。儘管如此,你可以這樣做,以避免使用list()函數。 'state.x77'是一個已經附加的數據集,因此你可以像這個例子一樣使用它。 – Jimbou