Q:

Simple R command not returning all values

SOLVED Thanks for your help everyone!
I think I am making a simple mistake that I have been staring at for too long to see. I have a dataframe with 4 classes (stored as numeric). Columns represent sites, rows represent dates. However, the sites are not evenly distributed, so I incorporated the distance between each site and its neighbouring site in the final row. The issue that I am having comes from an attempt to iterate through the dataframe, and sum the total distances for each class: 
dist_prop = data.frame(cbind(1:4, 0))

for (i in 1:(nrow(df)-1)){
  for (j in 1:(ncol(df))){
    dist_prop[(df[j,i]),2] = dist_prop[(df[j,i]),2]+df[nrow(df),j]
  }
}

I have looked into different parts of the code and simply cannot find where the error is. It is returning values, but they are much too low.
Thank you for any help you can offer.
edit:
Unfortunately, I cannot share the data. But it is in this format:
df = cbind(c(1,1,1,1,2,2,3), 
           c(1,1,2,2,3,3,4), 
           c(1,2,3,3,3,3,4), 
           c(1,1,1,1,1,2,3)) # classes
df = data.frame(rbind(df, c(0.2, 0.8, 1.2, 0.4))) # distances

I aimed to get the total distance occupied by each class by summing the corresponding distance values for each class. Instead, it returns lower values: 1: 6.4, 2: 2.4, 3: 1.6, 4: 0
The expected result is calculations of the following text:
     column 1 column 2 column 3 column 4 TOTAL
[1,] "4*0.2"  "2*0.8"  "1*1.2"  "5*0.4"  "5.6"
[2,] "2*0.2"  "2*0.8"  "1*1.2"  "1*0.4"  "4.8"
[3,] "1*0.2"  "2*0.8"  "4*1.2"  "1*0.4"  "7"  
[4,] "0*0.2"  "1*0.8"  "1*1.2"  "0*0.4"  "2" 

I am aiming for the code to return the TOTAL.
I hope this clears it up a little and provides a decent-ish example. I believe it hits the minimal, complete and verifiable criteria, as it seemed to replicate the problem when I ran the example.
Sorry, made a mistake with total 2. Corrected now. The totals are the class totals (total 1 = column 1 1 + column 2 1 + column 3 1 + column 4 1)

A:

If I understood your question right you are tabulating/counting the values 1, 2, 3 and 4 in each column of you original matrix. Then you are doing a matrix multiplication with the distances:
df0 <- cbind(c(1,1,1,1,2,2,3), c(1,1,2,2,3,3,4), c(1,2,3,3,3,3,4), c(1,1,1,1,1,2,3)) # classes
D <-  c(0.2, 0.8, 1.2, 0.4) # distances
# apply(df0, 2, function(x) tabulate(x,4)) ## the counting
apply(df0, 2, function(x) tabulate(x,4)) %*% D
# > apply(df0, 2, function(x) tabulate(x,4)) %*% D
#      [,1]
# [1,]  5.6
# [2,]  3.6
# [3,]  7.0
# [4,]  2.0

If you want a vector:
c(apply(df0, 2, function(x) tabulate(x,4)) %*% D)
# > c(apply(df0, 2, function(x) tabulate(x,4)) %*% D)
# [1] 5.6 3.6 7.0 2.0

or using sweep() instead of the matrix multiplication:
rowSums(sweep(apply(df0, 2, function(x) tabulate(x,4)), 2, STATS=D, FUN='*'))
# > rowSums(sweep(apply(df0, 2, function(x) tabulate(x,4)), 2, STATS=D, FUN='*'))
# [1] 5.6 3.6 7.0 2.0

or
rowSums(apply(df0, 2, function(x) tabulate(x,4)) * rep(D, each=4))
# > rowSums(apply(df0, 2, function(x) tabulate(x,4)) * rep(D, each=4))
# [1] 5.6 3.6 7.0 2.0

