0

I'm using plyr package in my script. It is required to join two data frames using the join function. Again in later part of script, many functions like rename, or some statistical functions don't work in presence of plyr package. I need to detach it, so that I get the required results. Is any way to give a command in between to remove the plyr package? My initial code is as follows:

setwd("C:/Ashish_PhD_dt_14062017/SPARC_COURSE/Meshlium_Field_Test 24022021_onwards")
install.packages("tidyverse")
library(tidyverse)
library(readr)
library(dplyr)
library(plyr)
library(purrr)
library(rlang)
library(lubridate)
PSKNK_MORNING <- read_csv("FILTERED_DATA/psknk_all_filtered_morning.csv") %>%
  select(TimeStamp,MAC,MeshliumID)
  
View(PSKNK_MORNING)
GNDIT_MORNING <- read_csv("FILTERED_DATA/gndit_all_filtered_morning.csv") %>%
  select(TimeStamp,MAC,MeshliumID) %>% 
  rename(TimeStamp1 = TimeStamp,GND_ID = MeshliumID)
View(GNDIT_MORNING)
SPEED_MORNING <- PSKNK_MORNING %>%
  inner_join(GNDIT_MORNING) %>%
  filter(day(TimeStamp) == day(TimeStamp1)) %>% 
  mutate(TimeDiff = (TimeStamp - TimeStamp1)/60, speed_kmph = 0.5 / as.numeric(TimeDiff) *60, Direction = ifelse(speed_kmph >= 0, "PS_KNK to GNDIT", "GNDIT to PS_KNK"), PCT = ntile(abs(speed_kmph),4))
View(SPEED_MORNING)

Rename doesnt work with plyr installed, so I have to remove it. When I go to join the dataframes, I require plyr. Furhter when when I find the statistical data, expected results are not shown, in presence of plyr. Again, I have to remove it.

 Device_Detection_Freq_evening <-Filtered_Speed_V_eve %>% 
+   group_by(date(TimeStamp), Direction) %>% 
+   count(MAC)
Error in count(., MAC) : object 'MAC' not found
> detach("package:plyr", unload=TRUE)
> Speed_Stat_Evening<- Filtered_Speed_V_eve %>% 
+   group_by(date(TimeStamp), Direction) %>% 
+   summarise(mean_speed = abs(mean(speed_kmph)),min_speed = min(abs(speed_kmph)), max_speed = max(abs(speed_kmph)),
+             median_speed = abs(median(speed_kmph)), Std_dev_speed = abs(sd(speed_kmph)))  
`summarise()` has grouped output by 'date(TimeStamp)'. You can override using the `.groups` argument.
> DeviceCount_Evening_datewise <-Filtered_Speed_V_eve %>% 
+   group_by(date(TimeStamp), Direction) %>% 
+   count()
> Device_Detection_Freq_evening <-Filtered_Speed_V_eve %>% 
+   group_by(date(TimeStamp), Direction) %>% 
+   count(MAC)

As you can see above, the count function works with plyr detached. How do I change the code to take care of this attaching and detaching plyr package? The sample data is as follows:

head(PSKNK_EVENING)
            TimeStamp               MAC   MeshliumID
1 2021-02-25 18:57:27 14:5E:69:C1:D6:4E 1.914211e+13
2 2021-02-25 18:57:27 20:CD:6E:07:50:5E 1.914211e+13
3 2021-02-25 18:57:27 30:23:53:FB:23:CA 1.914211e+13
4 2021-02-25 18:57:27 50:29:F5:EF:48:AA 1.914211e+13
5 2021-02-25 18:57:27 58:85:E9:AC:1A:3A 1.914211e+13
6 2021-02-25 18:57:27 70:5E:55:E9:25:EE 1.914211e+13
> head(GNDIT_EVENING)
           TimeStamp1               MAC       GND_ID
1 2021-02-26 18:21:45 58:85:A2:CD:B8:26 1.914211e+13
2 2021-02-26 18:21:46 00:18:CE:15:1B:40 1.914211e+13
3 2021-02-26 18:21:46 D2:F9:AF:1D:47:EA 1.914211e+13
4 2021-02-26 18:22:02 3B:7A:00:0B:B0:57 1.914211e+13
5 2021-02-26 18:22:05 76:4C:1A:99:62:61 1.914211e+13
6 2021-02-26 18:22:05 74:D2:1D:A8:9D:8B 1.914211e+13
> dput(head(PSKNK_EVENING))
structure(list(TimeStamp = structure(c(1614279447, 1614279447, 
1614279447, 1614279447, 1614279447, 1614279447), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), MAC = c("14:5E:69:C1:D6:4E", "20:CD:6E:07:50:5E", 
"30:23:53:FB:23:CA", "50:29:F5:EF:48:AA", "58:85:E9:AC:1A:3A", 
"70:5E:55:E9:25:EE"), MeshliumID = c(19142113152525, 19142113152525, 
19142113152525, 19142113152525, 19142113152525, 19142113152525
)), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))
> dput(head(GNDIT_EVENING))
structure(list(TimeStamp1 = structure(c(1614363705, 1614363706, 
1614363706, 1614363722, 1614363725, 1614363725), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), MAC = c("58:85:A2:CD:B8:26", "00:18:CE:15:1B:40", 
"D2:F9:AF:1D:47:EA", "3B:7A:00:0B:B0:57", "76:4C:1A:99:62:61", 
"74:D2:1D:A8:9D:8B"), GND_ID = c(19142113320526, 19142113320526, 
19142113320526, 19142113320526, 19142113320526, 19142113320526
)), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))
  • 1
    Search your title and you'd find [`detach`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/detach.html)`("package:tools")` as an example of detaching a package. – r2evans Mar 27 '22 at 12:33
  • You should simplify your example so that it is self-contained. We don't have the file you are reading, so we can't reproduce your error, but you should be able to construct similar datasets to `PSKNK_MORNING` and `GNDIT_MORNING` that have the same issues. – user2554330 Mar 27 '22 at 12:34
  • 3
    You load `plyr` after `dplyr`, and that is the problem. The standing recommendation (7+ years) is to *always* load `plyr` before `dplyr`. See https://stackoverflow.com/q/31644739/3358272. Lacking that, use `dplyr::count` and `dplyr::summarize` instead of just `count` and `summarize`, that will always choose the correct-package function you want. – r2evans Mar 27 '22 at 12:40
  • 2
    rather than loading plyr at all, you can use a function from its namespace like so: `plyr::function()`. – zephryl Mar 27 '22 at 12:41
  • While I believe that this question is a dupe of that previous link, your statement *"When I go to join the dataframes, I require plyr"* confuses me. Why does your use of `inner_join` require `plyr`? – r2evans Mar 27 '22 at 12:41
  • BTW, I see nothing in the code that demonstrates a need for `plyr`. – r2evans Mar 27 '22 at 13:05
  • r2evans, Thanks for your insights. I'm relatively new to R, hence have a lot of teething problems. Also the focus is to just get the data analysis part needed for my thesis to be completed. I've been able to extract most of the things required. have some problems intermittently. I'm working by trial and error, with online support tutorials, documentation etc. – Ashish Kulkarni Mar 27 '22 at 14:58
  • Thanks @zephryl, for your insightful comments. I have added the data for your understanding as well. All my questions are related to the above data only. – Ashish Kulkarni Mar 27 '22 at 15:12
  • @user2554330, the data sets mentioned by you are mine only. I have reproduced some part here as well. – Ashish Kulkarni Mar 27 '22 at 15:13
  • @AshishKulkarni: You should post code that we can run that produces the errors you saw. We can't run the very first line of your code, because we don't have that directory. You also need to edit your code so it doesn't depend on reading your files. – user2554330 Mar 27 '22 at 17:34
  • @user2554330, can you give a sample example? Do I need to give the data frames or what way should I include the csv sample files, so that You can run them? I have shown the data frames using dput. The files are quite large. – Ashish Kulkarni Mar 27 '22 at 18:07
  • See https://stackoverflow.com/q/5963269/2554330 . – user2554330 Mar 27 '22 at 18:44

0 Answers0