Below is my scenerio.
Scenerio
I have two dataframe. 1st dataframe contains data about system usage and another dataframe contains data about System location. I would like to track instrument usage based on date the system was used and also the location where the instrument is located. For this I am performing outer join on dataframes using dplyr
library. Next, I would like to get frequency count of the systems based on date. For this I am using groupby
on System and Locations. If the system is not in use the frequency count for that system should be 0.However, when I look at System 6, which is at location 3. Since, the instrument is not in use(No Date~assume not in use), the frequency count for that system should be 0, because Date or User column does not contain any data. However, below code is returning frequency count of 1. I am not sure, what could be wrong.Below is current and expected output.
Provide explanation with code.
Dataframe 1:
df <- data.frame("Users" =c('A',"B","A",'C','B'), "Date" = c('17-03-2019','15-03-2019','11-03-2019','20-04-2019',"21-04-2019"), "Systems" = c("Sys1", "Sys1","Sys2","Sys3","Sys4"), stringsAsFactors = FALSE)
df
Users Date Systems
1 A 17-03-2019 Sys1
2 B 15-03-2019 Sys1
3 A 11-03-2019 Sys2
4 C 20-04-2019 Sys3
5 B 21-04-2019 Sys4
Dataframe 2
loc_df<-data.frame("Locations" =c('loc1','loc1','loc2','loc2','loc3'),"Systems" = c("Sys1","Sys2","Sys3","Sys4","Sys6"), stringsAsFactors = FALSE)
loc_df
Locations Systems
1 loc1 Sys1
2 loc1 Sys2
3 loc2 Sys3
4 loc2 Sys4
5 loc3 Sys6
Frequency Count code
#Merging df
merge_df<-join(df, loc_df,type = "full")
#Replcaing NA's with 0
merge_df[is.na(merge_df)] <- 0
merge_df
#Code for frequency count
merge_df %>%
group_by(Systems,Locations)%>%
summarise(frequency = n())
Current Output:
Systems Locations frequency
<chr> <chr> <int>
1 Sys1 loc1 2
2 Sys2 loc1 1
3 Sys3 loc2 1
4 Sys4 loc2 1
5 Sys6 loc3 1
Expected Output
Systems Locations frequency
<chr> <chr> <int>
1 Sys1 loc1 2
2 Sys2 loc1 1
3 Sys3 loc2 1
4 Sys4 loc2 1
5 Sys6 loc3 0