分組變量我通常喜歡用lapply()
代替for
循環:r分割功能 - 不包括在新的數據集
lx <- split(x, x$hr) #with the next step being lapply(lx, function(x) ...)).
但是,現在的lx
每個元素包括列hr
,這是低效的,因爲這信息已在names(lx)
。
所以現在我必須做到:
lx <- lapply(lx, function(X) select(X, -hr))
(另一種方法是:
HR <- unique(x$hr)
lx <- select(lx, -hr)
lx <- split(x, HR)
)
的lapply()
在for
環整點是有效率如此這些額外的線打擾我。這似乎是一個常見的用例,而且我的經驗表明,通常R
的效率更高,或者我錯過了一些東西。
這可以在單個函數調用或單線程中實現嗎?
編輯:具體例
DF <- data.frame(A = 1:2, B = 2:3, C = 3:4)
DF <- split(DF, factor(DF$A)) # but each list element still contains the column A which is
# redundant (because the names() of the list element equals A
# as well), so I have to write the following line if I want
# to be efficient especially with large datasets
DF <- lapply(DF, function(x) select(x, -A)) # I hate always writing this line!
好的,我做到了。不知道是否有更好的解決方案。這似乎很常見.. – StatSandwich 2014-11-22 02:48:10