How to get frequency count of date based on condition in R?

丶灬走出姿态 提交于 2019-12-08 05:42:27

问题


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

回答1:


As the NAs are already changed to 0 (merge_df[is.na(merge_df)] <- 0), we can do a logical evaluation and get the sum instead of n(), which will return the number of rows and here the row is already present

library(dplyr)
merge_df %>% 
   group_by(Systems, Locations) %>%
   summarise(frequeency = sum(Date != 0))
# A tibble: 5 x 3
# Groups:   Systems [5]
#  Systems Locations frequeency
#  <chr>   <chr>          <int>
#1 Sys1    loc1               2
#2 Sys2    loc1               1
#3 Sys3    loc2               1
#4 Sys4    loc2               1
#5 Sys6    loc3               0

Instead of changing it to 0, it could also be done with sum(!is.na(Date)) as NA is more appropriate than 0



来源:https://stackoverflow.com/questions/57233371/how-to-get-frequency-count-of-date-based-on-condition-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!