1 year ago

#341289

test-img

Ashish Kulkarni

How to attach and remove a package in a script, when we require it for some part but it becomes problems for other part of script

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"
))

r

count

plyr

0 Answers

Your Answer

Accepted video resources