I have a dataframe CLIENTS with: Hotel number Nationality Date of arrive Date of departure Something like:
Client Nationality Hotel Dateofarrive DateofDeparture
Cl1 es h1 21/07/2019 24/07/2019
Cl2 es h1 23/07/2019 24/07/2019
Cl3 es h1 06/07/2019 10/07/2019
Cl4 es h2 05/07/2019 06/07/2019
Cl5 fr h3 01/07/2019 02/07/2019
Cl6 pt h1 07/07/2019 09/07/2019
And I created a dataframe Hotel and for each hotel of the dataframe i have all the clients that are of spanish nationality and all the nights they have sleept in the hotel, the same for french nationality and portuguese. Something like:
Hotel CliEspan Nights CliFrench Night CliPortug Night
H1 3 8 0 0 1 2
H2 1 5 1 1 0 0
Of all the nights the clients stay in the hotel I also want the number of nights that has been on a weekend. for example:
Hotel CliEspan Nights WdN CliFrench Night WdN CliPortug Night WdN
H1 3 8 5 0 0 0 1 2 2
H2 1 5 3 1 1 0 0 0 0
For the moment I have this, i need to include the weekends days.
clients[, nights := as.numeric(CL_DATASORTIDA - CL_DATAENTRADA)]
clients$CL_NACIONALITAT<-as.factor(as.character(clients$CL_NACIONALITAT))
clients$CL_NACIONALITAT<-substring(clients$CL_NACIONALITAT, 1,2)
clients$AT_REGISTRECOMERC<-substring(clients$AT_REGISTRECOMERC, 1,6)
# dcast to wide format
suma <- function(x)base::sum(x, na.rm=TRUE)
hotel <- dcast(clients, AT_REGISTRECOMERC ~ CL_NACIONALITAT, value.var = 'nights', fun.aggregate = list(suma, length))
names(hotel) <- gsub('nights_length', 'clients', names(new))
hotel<- data.frame(new)
inds <- which(colSums(hotel[, 90:177], na.rm=TRUE) < 20)
hotel$nights_other<-rowSums(hotel[, as.numeric(inds) + 1], na.rm=TRUE)
hotel$visitants_other<-rowSums(hotel[, as.numeric(inds) + 89], na.rm=TRUE)
hotel<-hotel[-c(inds+1, inds+89)]