Skip to content

Commit 45e74c1

Browse files
author
Lennart Noordermeer
committed
Edited priceMatrices
1 parent b949f48 commit 45e74c1

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

R/getPriceMatrices.R

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,57 @@
77
#' @seealso buckStem
88
#' @author Lennart Noordermeer \email{lennart.noordermeer@nmbu.no}
99
#' @export
10-
getPriceMatrices=function(XMLNode){
11-
require(XML);require(plyr);require(dplyr)
12-
a=XMLNode[["Machine"]][names(xmlSApply(XMLNode[["Machine"]],
13-
xmlAttrs)) == "ProductDefinition"]
14-
productdata=c()
15-
price_matrices=list()
16-
i=6
17-
for(i in 1:length(a)){
18-
ProductKey=xmlValue(a[[i]][["ProductKey"]])
19-
ProductName=xmlValue(a[[i]][["ClassifiedProductDefinition"]][["ProductName"]])
10+
getPriceMatrices <- function(XMLNode){
11+
require(XML); require(plyr); require(dplyr)
12+
13+
a <- XMLNode[["Machine"]][names(xmlSApply(XMLNode[["Machine"]], xmlAttrs)) == "ProductDefinition"]
14+
price_matrices <- list()
15+
16+
for(i in seq_along(a)){
17+
ProductKey <- xmlValue(a[[i]][["ProductKey"]])
18+
ProductName <- xmlValue(a[[i]][["ClassifiedProductDefinition"]][["ProductName"]])
19+
2020
if(!is.na(ProductName)){
21-
matrixlist= xmlToList(a[[i]][["ClassifiedProductDefinition"]][["ProductMatrixes"]])
22-
l=a[[i]][["ClassifiedProductDefinition"]][["ProductMatrixes"]]
23-
prices=dCLL=lCLL=numeric(length(l))
24-
m=1
25-
for(m in 1:length(l)){
26-
Item=l[[m]] %>% xmlToList()
27-
prices[m]=Item$Price %>% as.numeric()
28-
dCLL[m]=Item$.attrs[1] %>% as.numeric()
29-
lCLL[m]=Item$.attrs[2] %>% as.numeric() %>% round_any(10,floor)
21+
l <- a[[i]][["ClassifiedProductDefinition"]][["ProductMatrixes"]]
22+
if(length(l) == 0) next
23+
24+
dCLL <- numeric(length(l))
25+
lCLL <- numeric(length(l))
26+
price <- numeric(length(l))
27+
28+
for(m in seq_along(l)){
29+
Item <- l[[m]] %>% xmlToList()
30+
price[m] <- as.numeric(Item$Price)
31+
dCLL[m] <- as.numeric(Item$.attrs[1])
32+
lCLL[m] <- as.numeric(Item$.attrs[2])
3033
}
31-
m=matrix(prices,
32-
length(unique(lCLL)),
33-
length(unique(dCLL)),
34-
byrow = F)
35-
colnames(m)=unique(dCLL)
36-
rownames(m)=unique(lCLL)
37-
price_matrices[[ProductKey]]=m
34+
35+
# round length classes to dm (10 cm), then aggregate duplicates
36+
lCLL_r <- round_any(lCLL, 10, f = floor)
37+
38+
df <- data.frame(
39+
lCLL = lCLL_r,
40+
dCLL = dCLL,
41+
price = price
42+
) %>%
43+
group_by(lCLL, dCLL) %>%
44+
summarise(price = max(price, na.rm = TRUE), .groups = "drop")
45+
46+
l_levels <- sort(unique(df$lCLL))
47+
d_levels <- sort(unique(df$dCLL))
48+
49+
# fill full grid (missing combos become NA)
50+
grid <- expand.grid(lCLL = l_levels, dCLL = d_levels)
51+
grid <- left_join(grid, df, by = c("lCLL", "dCLL"))
52+
53+
mtx <- matrix(grid$price, nrow = length(l_levels), ncol = length(d_levels), byrow = FALSE)
54+
rownames(mtx) <- l_levels
55+
colnames(mtx) <- d_levels
56+
57+
price_matrices[[ProductKey]] <- mtx
3858
}
3959
}
40-
price_matrices=append(price_matrices,
41-
list('999999'=matrix(0,1,1,
42-
dimnames=list(0,0))))#waste
60+
61+
price_matrices <- append(price_matrices, list("999999" = matrix(0, 1, 1, dimnames = list(0, 0))))
4362
return(price_matrices)
4463
}

0 commit comments

Comments
 (0)