0

I have the following code to edit my the date and time in my data. I'm trying to save the results into df11 and df22 but when I double click on df11 and df22 in the environment (top right corner of RStudio) to view the data frames, they don't show me the updated results. It shows me the same tables/data that is in df1 and df2 as if they were never modified.

df1 <- structure(list(UserID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L), 
                      Full.Name = c( "John Smith", "Jack Peters", "Bob Brown", "Jane Doe", "Jackie Jane", "Sarah Brown", "Chloe Brown", "John Smith" ), 
                      Info = c("yes", "no", "yes", "yes", "yes", "yes", "no", "yes"), 
                      EncounterID = c(13L, 14L, 15L, 16L, 17L, 18L, 19L, 13L), DateTime = c("1/2/21 00:00", "1/5/21 12:00", "1/1/21 1:31", "1/5/21 3:34", "5/9/21 5:33", "5/8/21 3:39", "12/12/21 2:30", "12/11/21 9:21"), 
                      Temp = c("100", "103", "104", "103", "101", "102", "103", "105"), 
 
                      misc = c("(null)", "no", "(null)", "(null)", "(null)","(null)", "(null)", "(null)" 
                                    )), 
                 class = "data.frame", row.names = c(NA, 
                                                     -8L))

df2 <- structure(list(UserID = c(1L, 2L, 3L, 4L, 5L, 6L), 
                      Full.Name = c("John Smith", "Jack Peters", "Bob Brown", "Jane Doe", "Jackie Jane", "Sarah Brown"), 
                      DOB = c("1/1/90", "1/10/90", "1/2/90", "2/20/80", "2/2/80", "12/2/80"), 
                      EncounterID = c(13L, 14L, 15L, 16L, 17L, 18L), EncounterDate = c("1/1/21", "1/2/21", "1/1/21", "1/6/21", "5/7/21", "5/8/21"), 
                      Type = c("Intro", "Intro", "Intro", "Intro", "Care", "Out"), 
                      responses = c("(null)", "no", 
                                    "yes", "no", "no", "unsat")), 
                      
                 class = "data.frame", row.names = c(NA, 
                                                     -6L))
loadedNamespaces()
install.packages("Rcpp")
library(dplyr)
library(tidyr)
install.packages("lubridate")
library(lubridate)

df11 <- 
df1 %>% 
  separate(DateTime, c("Date", "Time"), sep=" ") %>% 
  mutate(Date = as_datetime(mdy(Date))) %>% 
  select(-Time) %>% 
  as_tibble()

df22 <-
df2 %>% 
  mutate(across(c(EncounterDate), mdy)) %>% 
  mutate(across(c(EncounterDate), as_datetime)) %>% 
  as_tibble()

I've tried using library(magrittr) and magrittr's compound assignment pipe-operator - %<>% as suggested in this post but it didn't work.

Ash S
  • 119
  • 5
  • This is [the same question as previous](https://stackoverflow.com/questions/69834624/r-dataframe-not-storing-tables-data) which was closed. Do the comments there not help? I ran your code and df11 does contain the transformed data from df1, so I'm not sure what the issue is. – neilfws Nov 05 '21 at 02:29
  • The comments there didn't help. It's strange that df11 contains the transformed data from df1 for you but not for me. – Ash S Nov 05 '21 at 04:07
  • It certainly is strange because the data and code in your question are quite reproducible (well done there) and results in `df11` containing a column `Date` of type `dttm`, as expected. There is no reason I can think of that you should not get the same result. – neilfws Nov 05 '21 at 04:14
  • Do you also get the transformed data from df2 in df22? – Ash S Nov 05 '21 at 04:30
  • Yes, `EncounterDate` becomes type `dttm`, as expected. As I say, there is nothing in your code or data to explain why df11 and df22 would not contain the transformed column. – neilfws Nov 05 '21 at 04:33

0 Answers0