Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- l.df <- lapply(ls(), function(x) if (class(get(x)) == "data.frame") get(x))
- library(dplyr);
- file_names <- dir() # Location of individual sales files
- agg_df <- do.call(rbind,lapply(file_names,read.csv))
- # Median sale per Article ID
- agg_df = agg_df %>% group_by(ArticleID) %>%
- mutate(mSlByArtId = median(AmountSold));
- # Median sale per Shop ID
- agg_df = agg_df %>% group_by(ShopID) %>%
- mutate(mSlByShpId = median(AmountSold));
- dl <- list()
- dl[[1]] <- data.frame(
- shopId = rep(1:4, each = 2),
- ArticleId = c(1, 1, 3, 2, 3, 2, 1, 2),
- date = 1:8,
- AmountSoled = 5
- )
- dl[[2]] <- data.frame(
- shopId = rep(1:4, each = 2),
- ArticleId = c(2, 1, 3, 2, 4, 4, 3, 1),
- date = 1:8,
- AmountSoled = 5
- )
- # dl
- # [[1]]
- # shopId ArticleId date AmountSoled
- # 1 1 1 1 5
- # 2 1 1 2 5
- # 3 2 3 3 5
- # 4 2 2 4 5
- # 5 3 3 5 5
- # 6 3 2 6 5
- # 7 4 1 7 5
- # 8 4 2 8 5
- #
- # [[2]]
- # shopId ArticleId date AmountSoled
- # 1 1 2 1 5
- # 2 1 1 2 5
- # 3 2 3 3 5
- # 4 2 2 4 5
- # 5 3 4 5 5
- # 6 3 4 6 5
- # 7 4 3 7 5
- # 8 4 1 8 5
- df <- do.call(rbind, dl)
- df
- # shopId ArticleId date AmountSoled
- # 1 1 1 1 5
- # 2 1 1 2 5
- # 3 2 3 3 5
- # 4 2 2 4 5
- # 5 3 3 5 5
- # 6 3 2 6 5
- # 7 4 1 7 5
- # 8 4 2 8 5
- # 9 1 2 1 5
- # 10 1 1 2 5
- # 11 2 3 3 5
- # 12 2 2 4 5
- # 13 3 4 5 5
- # 14 3 4 6 5
- # 15 4 3 7 5
- # 16 4 1 8 5
- aggregate(df, by = list(df$shopId), mean)
- # Group.1 shopId ArticleId date AmountSoled
- # 1 1 1 1.25 1.5 5
- # 2 2 2 2.50 3.5 5
- # 3 3 3 3.25 5.5 5
- # 4 4 4 1.75 7.5 5
Add Comment
Please, Sign In to add comment