pkframe2 <-subvisit3%>% arrange(SubjectID,PSCHDAY,ACTHOUR) %>%…
- User
pk_frame2 <-sub_visit3%>% arrange(Subject_ID,PSCHDAY,ACTHOUR) %>% left_join(pk_frame1,by="Subject_ID")%>%
mutate( #trtstm = as.POSIXct(anchor_time,format = "%H:%M"), visit_date = anchor + days(PSCHDAY - 1), collection_time =trtstm + as.difftime(ACTHOUR, units = "hours"), #collection_time1 = as.POSIXct(collection_time, format = "%H:%M"),
collection_time1 = with_tz(collection_time1, tz = "UTC"),
random_minutes = ifelse(PSCHHOUR %% 24 == 0 & PSCHMIN>0, sample(0:120, length(collection_time), replace = TRUE), sample(-15:0, length(collection_time), replace = TRUE)), ifelse(PSCHHOUR %% 24 == 0 & PSCHMIN<0, sample(-120:0, length(collection_time), replace = TRUE), sample(0:15, length(collection_time), replace = TRUE)), shifted_time = collection_time + lubridate::minutes(random_minutes), SPECTIM = format(shifted_time, "%H:%M"), SPECDAT = visit_date,) how to fix this code to show when PSCHHOUR %%24==0 AND PSCHMIN<0 THEN RANDOM_MINS RANGE IS -120:0, AND WHEN PSCHHOUR %%24==0 AND PSCHMIN>0 THEN RANDOM_MINIS RANGE IS 0:120, AND IF PSCHHOUR%%24 !=0 AND PSCHMIN<0 THEN RANDOM_MINS RANGE IS -15:0 AND WHEN PSCHHOUR %%24!=0 AND PSCHMIN>0 THEN RANDOM_MINS RANGE IS 0:15
- Assistant
Here's the corrected code with the specified conditions for the random_minutes ranges:
pk_frame2 <- sub_visit3 %>% arrange(Subject_ID, PSCHDAY, ACTHOUR) %>% left_join(pk_frame1, by = "Subject_ID") %>% mutate( visit_date = anchor + days(PSCHDAY - 1), collection_time = trtstm + as.difftime(ACTHOUR, units = "hours"), random_minutes = ifelse(PSCHHOUR %% 24 == 0 & PSCHMIN < 0, sample(-120:0, length(collection_time), replace = TRUE), ifelse(PSCHHOUR %% 24 == 0 & PSCHMIN > 0, sample(0:120, length(collection_time), replace = TRUE), ifelse(PSCHMIN < 0, sample(-15:0, length(collection_time), replace = TRUE), sample(0:15, length(collection_time), replace = TRUE) ) ) ), shifted_time = collection_time + lubridate::minutes(random_minutes), SPECTIM = format(shifted_time, "%H:%M"), SPECDAT = visit_date ) - User
interval <- sub_trt3 %>% group_by(Subject_ID, trtpn) %>% mutate(interval = difftime(datetime, lag(datetime), units = "hours")) %>% group_by(Subject_ID, trtpn) %>% summarise(median_interval_same = ifelse(is.na(interval), 999, median(interval, na.rm = TRUE)))%>% group_by(trtpn) %>% summarise(median_interval_diff = median(median_interval_same))%>% ungroup() median_interval <-list (interval $median_interval_diff) how to fix this code to ge t the median_interval list for each trtpn
- Assistant
To get the
median_intervallist for eachtrtpn, you can modify the code as follows:median_interval <- interval %>% group_by(trtpn) %>% summarise(median_interval_diff = median(median_interval_same)) %>% pull(median_interval_diff)This code groups the
intervaldata frame bytrtpnand calculates the median interval for each group using thesummarisefunction. The resulting data frame is then transformed to a vector using thepullfunction, extracting only themedian_interval_diffcolumn. The resulting vectormedian_intervalwill contain the median interval for eachtrtpnvalue. - User
interval <- sub_trt3 %>% group_by(Subject_ID, trtpn) %>% mutate(interval = difftime(datetime, lag(datetime), units = “hours”)) %>% group_by(Subject_ID, trtpn) %>% summarise(median_interval_same = ifelse(is.na(interval), 999, median(interval, na.rm = TRUE)))%>% group_by(trtpn) %>% summarise(median_interval_diff = median(median_interval_same))%>% ungroup() median_interval <-list (interval $median_interval_diff) how to fix this code to ge t the median_interval vector for each trtpn when we want to use interval_median[[i]] and i stands for trtpn
- Assistant
To get the
median_intervalvector for eachtrtpnwhere you can useinterval_median[[i]]andirepresentstrtpn, you can modify the code as follows:interval <- sub_trt3 %>% group_by(Subject_ID, trtpn) %>% mutate(interval = difftime(datetime, lag(datetime), units = "hours")) %>% group_by(Subject_ID, trtpn) %>% summarise(median_interval_same = ifelse(is.na(interval), 999, median(interval, na.rm = TRUE))) %>% group_by(trtpn) %>% summarise(median_interval_diff = median(median_interval_same)) %>% ungroup() median_interval <- as.list(interval$median_interval_diff)This code calculates the
median_intervalfor eachtrtpnas before. Then, usingas.list, it converts themedian_interval_diffcolumn from theintervaldata frame into a list. Now, you can access the median intervals usinginterval_median[[i]], whereirepresents thetrtpnvalue.Note: Make sure
trtpnis a categorical variable or factor for which each level corresponds to an index in the resulting list. - User
Unrecognized time zone 'Asia/Shanghai ' for timezone="Asia/Shanghai"
- Assistant
It seems there is an extra space at the end of the time zone value "Asia/Shanghai ". You need to remove that space to avoid the "Unrecognized time zone" error.
Update the code to:
mutate( # ... collection_time = with_tz(collection_time, tz = "Asia/Shanghai") # ... )Make sure there are no additional spaces in the time zone value, and it should resolve the error.