I am looking at a dataset where I got companies, and what there prices are for several weeks. If the value is blank/empty it is due to the house is booked and therefore there is no price available.
I have this code which is working, but I want to do all companies and weeks at once if possible. And then I want it to become a part of the data.
sum(D1$Company=='dc' & D1$`Price week 24`== " ") / sum(D1$Company=='dc' & D1$`Price week 24`!="-10")
Where I take the sum of one companies where the houses is booked (no price therefore blank/empty), and divide with the total. No values of -10..
My data could look like this (Sorry for the bad vision but I cannot paste in a screenshot). I got several more weeks and several companies. I could see a new column named "Occupancy week 24" where it contains the value according to the company in row 1.
EDIT : the data
# dput(DF1)
structure(list(Company = 1:6, Price_week_24 = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = "ns", class = "factor"), Price_week_25 = c(1639L,
860L, NA, NA, 399L, 645L), Price_week_26 = c(NA, 860L, NA, NA,
399L, NA), Price_week_27 = c(NA, 1010L, 1010L, 699L, 399L, 1010L
), Price_week_28 = c(NA, 1399L, NA, 1129L, 640L, 1399L)), class = "data.frame", row.names = c(NA,
-6L))
df$occupancy_rate <- apply(df[,2:6], 1,function(x) sum(x>0, na.rm = TRUE)/length(x)) Solve many problems but not them all. I want a value for every single company and not a total for them all.
I am looking forward to getting some help. Thank you.
Best Regards