Skip to main content

Calculating Wall Surface Areas in R on the Basis of Vectors

I have moved recently and was asked to paint the walls and ceilings of my old apartment by the former property management company. I went ahead right away and tried to get quotes from painting and decorating businesses online. There, I was asked immediately to provide the surface area to be painted in square-meters ... mmh, of course, I could have provided the floor area and the number of rooms straight away, and I had hoped that the businesses would use a simple projection for preparing the quotes. But to directly calculate the surface area to be painted appeared somewhat more complex to me than an off the cuff estimate.  

Solution with R

In the past, I would have solved the problem with an Excel spreadsheet, but this time, I wanted to use R as calculator. 

Stating the basic measurements

Firstly, I defined some key data as variables: 

zimmerhöhe<-2.8
anzahl_türen<-6 
anzahl_fenster<-5

Then I entered the floor area of the rooms. For calculating the ceiling area, of course, I can use the known square meters of the floor, but for the walls, I have to compute length and width of each room with the height of the ceiling. As I am thus combining length and width once multiplicatively and once additively, I have decided to firstly store all room measurements as vectors. 

grundflächen_räume<-list(kueche=c(3.4,3.7),
                         arbeitszimmer=c(3.0,5.0), 
                         wohnzimmer=c(3.0,5.0), 
                         bad=c(1.5,1.8), 
                         flur=c(1.1,2.5), 
                         wc=c(0.8,2.8), 
                         schlafzimmer=c(3.0,5.0))

Of course, I need to subtract doors and windows from the total area to be calculated later. I am taking the doors into account twice for both sides and subtract one side for the front door. The surface space is calculated via the prod() function which calculates the products of the elements of a vector, here the measurements in meters. 

türenfläche<-prod(c(2.10,1.06))*(anzahl_türen*2-1)
fensterfläche<-prod(c(1.6,1.0))*anzahl_fenster 

In order not to make it too easy, I also have to subtract tiled walls of various heights for the bathroom and the toilet. To this end, I again take the length and width of the rooms and multiply them with the measured height of the tiled space. Of course, I do not want to take the measures of the rooms into account twice and thus hold them redundantly, so I fetch the vectors from the room itemization above. 

fließen_wc<-sum(2*grundflächen_räume$wc) * 1.3
fließen_bad<-sum(2*grundflächen_räume$bad)* 2.0

Finally, I still need the kitchen unit, which in order to simplify matters covers only one wall: 

kuechzeile<-3.4*2.4

 

User defined function for wall surfaces

Now for the total wall space. I have encapsulated the calculation within a function. 

f_wandfläche<-function(grundflächen){ 
    prod(grundflächen)+sum(2*grundflächen)*zimmerhöhe;
}

 

Best Practice: Vectorization instead of loops

As I have worked with various programming languages in some depth over the years, primarily with Python, I was initially tempted to solve the calculation of all rooms via a loop. This would run over all rooms and apply the function to each element, much like this: 

wandfläche_gesamt = 0 
for(raum in grundflächen_räume){ 
    wandfläche_gesamt = wandfläche_gesamt + f_wandfläche(raum)
}

While this works perfectly, it is just the wrong approach in R. The background for this is that the loop carries out incremental repetitions. It is considerably more performant to calculate this in R on the basis of vectors. This is really negligible with our few rooms. But in case of larger datasets with thousands of lines, it does become noticeable. The following line uses the sapply function, which applies a function to a vector in one go and reports the result back as a vector. 

wandflächen<-sapply(grundflächen_räume,f_wandfläche) 
#Hier das Ergebnis 
#kueche arbeitszimmer wohnzimmer bad flur wc schlafzimmer 
#52.34 59.80 59.80 21.18 22.91 22.40 59.80 

Result

Unfortunately, I did not have the exact measurements of the old apartment and had to guess some of them. As a final check, however, the area should correspond to the actual space of approximately 65sqm. Accordingly, I again calculated the total area on the basis of the data by means of the sapply function and apply the prod function to the vector in order to calculate the product of length and width. 

grundfläche_gesamt<-sum(sapply(grundflächen_räume,prod)) 
#Das Ergebnis: 
#65.27 

For my final wall surface area, I now only have to add my vector of individual areas and subtract the special areas such as windows and doors. 

wandfläche_gesamt<-sum(wandflächen)-türenfläche-fensterfläche-fließen_wc-kuechzeile-fließen_bad 

So the result is approximately 235sqm.

P.S.: I have since had an on-site appointment with a painter. He roughly measured the apartment and calculated 220 sqm via a rule of thumb. Not bad either!

Stefan Seltmann
Stefan Seltmann
Lead Expert
Stefan loves programming, particularly when data engineering and data science are involved. He's turned his hobby into a career and has earned a reputation as a "phone a friend" whenever there's a tricky Python or Spark software development problem.
#CodeFirst, #TestMore, #CodeDoctor