indexing function in for loop

I wrote a working code to print the top 3 brands (among 6 brands) in each market (among 6 markets) by Sales Value (Rs).

s <- filter(data, market == "South" & fact == "Sales Value (Rs)" )
south <- data.frame( brand = c("B", "L", "Q", "H", "K", "S"), 
                   total_south_Rs = c(sum(s[which(s$brand=="B"), 18:29]),
                                       sum(s[which(s$brand=="L"), 18:29]),
                                       sum(s[which(s$brand=="Q"), 18:29]),
                                       sum(s[which(s$brand=="H"), 18:29]),
                                       sum(s[which(s$brand=="K"), 18:29]),
                                       sum(s[which(s$brand=="S"), 18:29]))

print(south[order(south$total_south_Rs, decreasing = TRUE),] [1:3,])

mt <- filter(data, market == "MT" & fact == "Sales Value (Rs)" )
MT <- data.frame( brand = c("B", "L", "Q", "H", "K", "S"), 
                   total_mt_Rs = c(sum(mt[which(mt$brand=="B"), 18:29]),
                                       sum(mt[which(mt$brand=="L"), 18:29]),
                                       sum(mt[which(mt$brand=="Q"), 18:29]),
                                       sum(mt[which(mt$brand=="H"), 18:29]),
                                       sum(mt[which(mt$brand=="K"), 18:29]),
                                       sum(mt[which(mt$brand=="S"), 18:29]))

print(MT[order(MT$total_mt_Rs, decreasing = TRUE),][1:3,])

# repeating the code 4 more times for 6 markets.

I want to implement a for loop to the otherwise lengthy code above by performing the sum of the sales value of each brand in one market, looping the market's data frame, and finally printing that data frame in descending order (top 3).

# importing the dataset
data <- read.csv(filename.csv")
markets <- unique(data$market)
brands <- unique(data$brand)
flavors <- unique(data$flavor)
pack_sizes <- unique(data$pack.size...gms)

for(i in 1:length(markets)){
  for (j in 1:length(brands)) {
    df[i] <- filter(data, market == "markets[i]" & fact == "Sales Value (Rs)")
    market_sales[i] <- data.frame(c(brand = "brands"),
                    total_market_Rs[i] <-
                      c(sum(markets[i][which(df[i]$brand == "brands[j]"), 18:29]))
    print(market_sales[i][order(markets[i]$total_market_Rs[i], decreasing = TRUE),][1:3,])


I get the error upon running the above code chunk,

Error in markets[i][which(df[i]$brand == "brands[j]"), 18:29] : 
  incorrect number of dimensions

am I going wrong with the indices? how to get this right.

Are you able to share the data?

# importing the dataset
data <- read.csv(filename.csv")
1 Like

I cannot share the data, is it not against the community policy?
i just want to implement a for loop for my repetitive code

It will almost certainly be more elegant cody for you to use tidyverse to group by and summarise.
As far as sharing representative data goes

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.