Reputation: 2498
I have the following data frame called groceries
:
Region Item Barcode Version
East Cereal BM 2.1
North Bagel EP 2.0
East Pizza BM 2.3
West Taco EP 2.2
West Apple BM 3.0
South Orange EP 3.2
North Tomato EP 2.2
South Grape EP 2.0
East Pineapple EP 3.2
North Cake BM 2.0
How do I subset the data frame by Barcode-Version to get the following data frames?
data frame
Region Item Barcode Version
East Cereal BM 2.1
East Pizza BM 2.3
North Cake BM 2.0
data frame
Region Item Barcode Version
West Apple BM 3.0
data frame
Region Item Barcode Version
North Bagel EP 2.0
North Tomato EP 2.2
South Grape EP 2.0
West Taco EP 2.2
data frame
Region Item Barcode Version
South Orange EP 3.2
East Pineapple EP 3.2
As you can see, I'm trying to subset the original data frame by barcode and version (as an integer; so 2.0, 2.1, 2.3 are all considered 2, etc.).
Here's what I have so far:
subset(groceries, Barcode=="BM" & Version==2.0 | Version==2.1 | Version==2.3)
As you can imagine this is not ideal. Is there a way that I can get vectors of the different factors for Barcode
and Version
(as integers)? If I can get those two as a vectors then I can probably create a for loop that automates the line above to create these 4 data frames.
Upvotes: 2
Views: 621
Reputation: 48241
split(df, interaction(df$Barcode, floor(df$Version)))
# $BM.2
# Region Item Barcode Version
# 1 East Cereal BM 2.1
# 3 East Pizza BM 2.3
# 10 North Cake BM 2.0
#
# $EP.2
# Region Item Barcode Version
# 2 North Bagel EP 2.0
# 4 West Taco EP 2.2
# 7 North Tomato EP 2.2
# 8 South Grape EP 2.0
#
# $BM.3
# Region Item Barcode Version
# 5 West Apple BM 3
#
# $EP.3
# Region Item Barcode Version
# 6 South Orange EP 3.2
# 9 East Pineapple EP 3.2
Upvotes: 3