I have a data set that I'd like to organize into a list but am unsure of the most efficient way to do so. It's a list of sales people and products sold:
rowId repNum prodNum
001 12006 335506883
002 12008 152348345
003 12055 993047386
004 12006 294726636
005 12069 110847537
006 12006 335506883
007 12006 152348345
... ... ...
I'd like to create a list where each element contains a vector of items that rep has sold, my initial idea was to use tapply but that needs a function to net by. Any other suggestions?
If your data.frame is called dataset,
split(dataset, dataset$repNum)
will create a list with a component for each repNum
> split(dataset, dataset$repNum)
$`12006`
rowId repNum prodNum
1 1 12006 335506883
4 4 12006 294726636
6 6 12006 335506883
7 7 12006 152348345
$`12008`
rowId repNum prodNum
2 2 12008 152348345
$`12055`
rowId repNum prodNum
3 3 12055 993047386
$`12069`
rowId repNum prodNum
5 5 12069 110847537
As pointed out by mrdwab, you can get "vector[s] of items that rep has sold" by splitting the prodNum like this:
> split(dataset$prodNum, dataset$repNum)
$`12006`
[1] 335506883 294726636 335506883 152348345
$`12008`
[1] 152348345
$`12055`
[1] 993047386
$`12069`
[1] 110847537
Using tapply(), you can do it like this:
with(dataset, tapply(prodNum, repNum, FUN=function(X) X))
# $`12006`
# [1] 335506883 294726636 335506883 152348345
#
# $`12008`
# [1] 152348345
#
# $`12055`
# [1] 993047386
#
# $`12069`
# [1] 110847537
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With