merge info, convert factors, create time variable

Greetings,

I have reduced a dataset to 4 variables, all of which were imported as factors.

> head(dat.time)
  bedtime bt.sleep waketime wt.wake
1   10:00     P.M.     5:00    A.M.
2   11:00     P.M.     6:30    A.M.
3   10:30     P.M.     7:00    A.M.
4   11:00     P.M.     7:00    A.M.
5   10:00     P.M.     7:30    A.M.
6   10:30     P.M.     7:00    A.M.
> class(dat.time$bedtime)
[1] "labelled" "factor"  
> class(dat.time$bt.sleep)
[1] "factor"
> class(dat.time$waketime)
[1] "labelled" "factor"  
> class(dat.time$wt.wake)
[1] "factor"
> 

I am trying to create 2 new variables

  1. Sleep - from columns 1 & 2
  2. Wake - from columns 3 & 4

My goal is to then calculate a third variable

  1. Time in bed

Which will be the difference of 2 - 1

I have tried several packages including

lubridate
tidyverse
#chron
#anytime

I tried these but the result input today's date.

dat.time$bedtime2  <- as.character(dat.time$bedtime)

dat.time$bedtime3 <- as.POSIXct(dat.time$bedtime2,
                       format = '%H:%M')

As an #rstatsnewbie, any help would be greatly appreciated.

Jason

For your next posts, please provide a reproducible example of the problems you face, and include your data in a copy paste friendly format.

For your first problem of converting factor to character, you did it correctly with as.character. The join can be done by paste, as below.

Now, for your next question, we first need to have 2 datetime objects, but you have nothing as such. You cannot have time difference only by hour and minute. Based on the provided information, how is one to know whether both are in same day or not. I'm providing a solution assuming same timezone, two consecutive dates (today and tomorrow) below, but you have to decide whether that is your case or not.

sample_data <- read.table(header = TRUE,
                          text = "  bedtime bt.sleep waketime wt.wake
1   10:00     P.M.     5:00    A.M.
2   11:00     P.M.     6:30    A.M.
3   10:30     P.M.     7:00    A.M.
4   11:00     P.M.     7:00    A.M.
5   10:00     P.M.     7:30    A.M.
6   10:30     P.M.     7:00    A.M.")

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

sample_data %>%
    mutate_all(.funs = as.character) %>%
    mutate_at(.vars = c("bt.sleep", "wt.wake"),
              .funs = ~ gsub(pattern = ".",
                             replacement = "",
                             x = .x,
                             fixed = TRUE)) %>%
    mutate(Sleep = as.POSIXct(x = paste(bedtime, bt.sleep),
                              format = "%I:%M %p"),
           Wake = as.POSIXct(x = paste(waketime, wt.wake),
                             format = "%I:%M %p") + as.difftime(tim = 1,
                                                                units = "days"),
           `Time in bed` = Wake - Sleep)
#>   bedtime bt.sleep waketime wt.wake               Sleep                Wake
#> 1   10:00       PM     5:00      AM 2020-04-01 22:00:00 2020-04-02 05:00:00
#> 2   11:00       PM     6:30      AM 2020-04-01 23:00:00 2020-04-02 06:30:00
#> 3   10:30       PM     7:00      AM 2020-04-01 22:30:00 2020-04-02 07:00:00
#> 4   11:00       PM     7:00      AM 2020-04-01 23:00:00 2020-04-02 07:00:00
#> 5   10:00       PM     7:30      AM 2020-04-01 22:00:00 2020-04-02 07:30:00
#> 6   10:30       PM     7:00      AM 2020-04-01 22:30:00 2020-04-02 07:00:00
#>   Time in bed
#> 1   7.0 hours
#> 2   7.5 hours
#> 3   8.5 hours
#> 4   8.0 hours
#> 5   9.5 hours
#> 6   8.5 hours

Hope this helps.

2 Likes

Thank you. I will try your suggestion and if needed will reply with the data.
Many thanks.
Jason

Thank you for the help! The code works, but now all I have to do is figure out how it works. :smile:
Unfortunately, I have run into another snag. Some people have gone to bed and awoke on the same day and thus looking like they have slept for more than 24 hours.
Any suggestions on how to address this issue would also be appreciated.

Many thanks to any/all who help.
Jason

structure(list(bedtime = c("10:00", "11:00", "10:30", "11:00", 
"10:00", "10:30", "11:30", "10:30", "10:30", "9:30", "10:30", 
"8:30", "10:30", "12:00", "12:30", "10:30", "12:00", "10:00", 
"11:00", "10:00"), bt.sleep = c("PM", "PM", "PM", "PM", "PM", 
"PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "AM", "AM", "PM", 
"AM", "PM", "PM", "PM"), waketime = c("5:00", "6:30", "7:00", 
"7:00", "7:30", "7:00", "7:00", "7:00", "6:00", "4:30", "7:30", 
"4:30", "6:30", "8:00", "7:30", "7:00", "8:00", "6:30", "7:45", 
"6:00"), wt.wake = c("AM", "AM", "AM", "AM", "AM", "AM", "AM", 
"AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", 
"AM", "AM"), Sleep = structure(c(1585796400, 1585800000, 1585798200, 
1585800000, 1585796400, 1585798200, 1585801800, 1585798200, 1585798200, 
1585794600, 1585798200, 1585791000, 1585798200, 1585717200, 1585719000, 
1585798200, 1585717200, 1585796400, 1585800000, 1585796400), class = c("POSIXct", 
"POSIXt"), tzone = ""), Wake = structure(c(1585821600, 1585827000, 
1585828800, 1585828800, 1585830600, 1585828800, 1585828800, 1585828800, 
1585825200, 1585819800, 1585830600, 1585819800, 1585827000, 1585832400, 
1585830600, 1585828800, 1585832400, 1585827000, 1585831500, 1585825200
), class = c("POSIXct", "POSIXt")), `Time in bed` = structure(c(7, 
7.5, 8.5, 8, 9.5, 8.5, 7.5, 8.5, 7.5, 7, 9, 8, 8, 32, 31, 8.5, 
32, 8.5, 8.75, 8), class = "difftime", units = "hours")), row.names = c(NA, 
20L), class = "data.frame")
1 Like

I think it is very obvious why this is happening. This is precisely why I mentioned the following:

You are getting more than 24 hours because for each pair of times, Sleep is considered in one day (the current day), and Wake is considered in next day, irrespective of the AM/PM information in bt.sleep. This is causing it to cross 24 hours, if someone goes to bed after midnight.

Given the data, I am unable to say for which observation both times are in same day, and for which those are not, without using my common sense. And unfortunately, it is beyond my scope to express my common sense in code.

I can give a suggestion to use modulo operator of 24 hours, but again, it is up to you. And if someone provides a better solution, I'll be very much interested.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

sample_data <- tibble(bedtime = c("10:00", "11:00", "10:30", "11:00", "10:00", "10:30", "11:30", "10:30", "10:30", "9:30", "10:30", "8:30", "10:30", "12:00", "12:30", "10:30", "12:00", "10:00", "11:00", "10:00"),
                      bt.sleep = c("PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "AM", "AM", "PM", "AM", "PM", "PM", "PM"),
                      waketime = c("5:00", "6:30", "7:00", "7:00", "7:30", "7:00", "7:00", "7:00", "6:00", "4:30", "7:30", "4:30", "6:30", "8:00", "7:30", "7:00", "8:00", "6:30", "7:45", "6:00"),
                      wt.wake = c("AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM"))

sample_data %>%
    mutate(Sleep = as.POSIXct(x = paste(bedtime, bt.sleep),
                              format = "%I:%M %p"),  # time in current date
           Wake = as.POSIXct(x = paste(waketime, wt.wake),
                             format = "%I:%M %p") + as.difftime(tim = 1,
                                                                units = "days"),  # time in current date plus one day, equivalently time in next day
           `Time Difference` = difftime(time1 = Wake,
                                        time2 = Sleep,
                                        units = "hours"),  # difference of the two times, guaranteed to be in hours
           `Time in bed` = as.numeric(x = `Time Difference`) %% 24) %>%  # difference of the two times, guaranteed to be in [0, 24)
    print(width = Inf)  # just to print all columns
#> # A tibble: 20 x 8
#>    bedtime bt.sleep waketime wt.wake Sleep               Wake               
#>    <chr>   <chr>    <chr>    <chr>   <dttm>              <dttm>             
#>  1 10:00   PM       5:00     AM      2020-04-02 22:00:00 2020-04-03 05:00:00
#>  2 11:00   PM       6:30     AM      2020-04-02 23:00:00 2020-04-03 06:30:00
#>  3 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  4 11:00   PM       7:00     AM      2020-04-02 23:00:00 2020-04-03 07:00:00
#>  5 10:00   PM       7:30     AM      2020-04-02 22:00:00 2020-04-03 07:30:00
#>  6 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  7 11:30   PM       7:00     AM      2020-04-02 23:30:00 2020-04-03 07:00:00
#>  8 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  9 10:30   PM       6:00     AM      2020-04-02 22:30:00 2020-04-03 06:00:00
#> 10 9:30    PM       4:30     AM      2020-04-02 21:30:00 2020-04-03 04:30:00
#> 11 10:30   PM       7:30     AM      2020-04-02 22:30:00 2020-04-03 07:30:00
#> 12 8:30    PM       4:30     AM      2020-04-02 20:30:00 2020-04-03 04:30:00
#> 13 10:30   PM       6:30     AM      2020-04-02 22:30:00 2020-04-03 06:30:00
#> 14 12:00   AM       8:00     AM      2020-04-02 00:00:00 2020-04-03 08:00:00
#> 15 12:30   AM       7:30     AM      2020-04-02 00:30:00 2020-04-03 07:30:00
#> 16 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#> 17 12:00   AM       8:00     AM      2020-04-02 00:00:00 2020-04-03 08:00:00
#> 18 10:00   PM       6:30     AM      2020-04-02 22:00:00 2020-04-03 06:30:00
#> 19 11:00   PM       7:45     AM      2020-04-02 23:00:00 2020-04-03 07:45:00
#> 20 10:00   PM       6:00     AM      2020-04-02 22:00:00 2020-04-03 06:00:00
#>    `Time Difference` `Time in bed`
#>    <drtn>                    <dbl>
#>  1  7.00 hours                7   
#>  2  7.50 hours                7.5 
#>  3  8.50 hours                8.5 
#>  4  8.00 hours                8   
#>  5  9.50 hours                9.5 
#>  6  8.50 hours                8.5 
#>  7  7.50 hours                7.5 
#>  8  8.50 hours                8.5 
#>  9  7.50 hours                7.5 
#> 10  7.00 hours                7   
#> 11  9.00 hours                9   
#> 12  8.00 hours                8   
#> 13  8.00 hours                8   
#> 14 32.00 hours                8   
#> 15 31.00 hours                7   
#> 16  8.50 hours                8.5 
#> 17 32.00 hours                8   
#> 18  8.50 hours                8.5 
#> 19  8.75 hours                8.75
#> 20  8.00 hours                8

Created on 2020-04-02 by the reprex package (v0.3.0)

3 Likes

Great solution @Yarnabrina! If I may, perhaps a little if_else() magic can help handle the bed times at/after midnight?

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 3.6.3

sample_data <- tibble(bedtime = c("10:00", "11:00", "10:30", "11:00", "10:00", "10:30", "11:30", "10:30", "10:30", "9:30", "10:30", "8:30", "10:30", "12:00", "12:30", "10:30", "12:00", "10:00", "11:00", "10:00"),
                      bt.sleep = c("PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "PM", "AM", "AM", "PM", "AM", "PM", "PM", "PM"),
                      waketime = c("5:00", "6:30", "7:00", "7:00", "7:30", "7:00", "7:00", "7:00", "6:00", "4:30", "7:30", "4:30", "6:30", "8:00", "7:30", "7:00", "8:00", "6:30", "7:45", "6:00"),
                      wt.wake = c("AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "AM"))

sample_data %>%
  mutate(Sleep = if_else(bt.sleep == "PM",
                         true = as.POSIXct(x = paste(bedtime, bt.sleep), 
                                           format = "%I:%M %p"), # time in current date
                         false = as.POSIXct(x = paste(bedtime, bt.sleep), 
                                            format = "%I:%M %p") + as.difftime(tim = 1, 
                                                                               units = "days")), # consider next day for times at/after midnight
         Wake = as.POSIXct(x = paste(waketime, wt.wake),
                           format = "%I:%M %p") + as.difftime(tim = 1,
                                                              units = "days"), # time in current date plus one day, equivalently time in next day
         `Time Difference` = difftime(time1 = Wake,
                                      time2 = Sleep,
                                      units = "hours")) %>%  # difference of the two times, guaranteed to be in [0, 24)
  print(width = Inf)
#> # A tibble: 20 x 7
#>    bedtime bt.sleep waketime wt.wake Sleep               Wake               
#>    <chr>   <chr>    <chr>    <chr>   <dttm>              <dttm>             
#>  1 10:00   PM       5:00     AM      2020-04-02 22:00:00 2020-04-03 05:00:00
#>  2 11:00   PM       6:30     AM      2020-04-02 23:00:00 2020-04-03 06:30:00
#>  3 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  4 11:00   PM       7:00     AM      2020-04-02 23:00:00 2020-04-03 07:00:00
#>  5 10:00   PM       7:30     AM      2020-04-02 22:00:00 2020-04-03 07:30:00
#>  6 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  7 11:30   PM       7:00     AM      2020-04-02 23:30:00 2020-04-03 07:00:00
#>  8 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#>  9 10:30   PM       6:00     AM      2020-04-02 22:30:00 2020-04-03 06:00:00
#> 10 9:30    PM       4:30     AM      2020-04-02 21:30:00 2020-04-03 04:30:00
#> 11 10:30   PM       7:30     AM      2020-04-02 22:30:00 2020-04-03 07:30:00
#> 12 8:30    PM       4:30     AM      2020-04-02 20:30:00 2020-04-03 04:30:00
#> 13 10:30   PM       6:30     AM      2020-04-02 22:30:00 2020-04-03 06:30:00
#> 14 12:00   AM       8:00     AM      2020-04-03 00:00:00 2020-04-03 08:00:00
#> 15 12:30   AM       7:30     AM      2020-04-03 00:30:00 2020-04-03 07:30:00
#> 16 10:30   PM       7:00     AM      2020-04-02 22:30:00 2020-04-03 07:00:00
#> 17 12:00   AM       8:00     AM      2020-04-03 00:00:00 2020-04-03 08:00:00
#> 18 10:00   PM       6:30     AM      2020-04-02 22:00:00 2020-04-03 06:30:00
#> 19 11:00   PM       7:45     AM      2020-04-02 23:00:00 2020-04-03 07:45:00
#> 20 10:00   PM       6:00     AM      2020-04-02 22:00:00 2020-04-03 06:00:00
#>    `Time Difference`
#>    <drtn>           
#>  1 7.00 hours       
#>  2 7.50 hours       
#>  3 8.50 hours       
#>  4 8.00 hours       
#>  5 9.50 hours       
#>  6 8.50 hours       
#>  7 7.50 hours       
#>  8 8.50 hours       
#>  9 7.50 hours       
#> 10 7.00 hours       
#> 11 9.00 hours       
#> 12 8.00 hours       
#> 13 8.00 hours       
#> 14 8.00 hours       
#> 15 7.00 hours       
#> 16 8.50 hours       
#> 17 8.00 hours       
#> 18 8.50 hours       
#> 19 8.75 hours       
#> 20 8.00 hours

Created on 2020-04-02 by the reprex package (v0.3.0)

P.S. Funnily, base::ifelse() gives a weird result with the same code. Possibly a bug?

1 Like

@siddharthprabhu Thanks for working on this, but for some reason I am not getting the same results. This is what prints to the console.

library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 3.6.3
dat.time2 <- dat.time %>%
    mutate(Sleep = if_else(bt.sleep == "PM",
                           true = as.POSIXct(x = paste(bedtime, bt.sleep), 
                                          format = "%I:%M %p"), # time in current date
                           false = as.POSIXct(x = paste(bedtime, bt.sleep),
                                           format = "%I:%M %p") + 
                            as.difftime(tim = 1, 
                                        units = "days")), # consider next day for times at/after midnight
           Wake = as.POSIXct(x = paste(waketime, wt.wake),
                          format = "%I:%M %p") + 
            as.difftime(tim = 1, units = "days"), # time in current date plus one day, equivalently time in next day
           `Time Difference` = difftime(time1 = Wake,
                                     time2 = Sleep,
                                     units = "hours")) %>%  # difference of the two times, guaranteed to be in [0, 24)
    print(width = Inf)
#> Error in eval(lhs, parent, parent): object 'dat.time' not found

Created on 2020-04-07 by the reprex package (v0.3.0)

This is what was printed to the console

# A tibble: 158 x 7
   bedtime bt.sleep waketime wt.wake Sleep               Wake               
   <fct>   <fct>    <fct>    <fct>   <dttm>              <dttm>             
 1 10:00   P.M.     5:00     A.M.    NA                  NA                 
 2 11:00   P.M.     6:30     A.M.    NA                  NA                 
 3 10:30   P.M.     7:00     A.M.    NA                  NA                 
 4 11:00   P.M.     7:00     A.M.    NA                  NA                 
 5 10:00   P.M.     7:30     A.M.    NA                  NA                 
 6 10:30   P.M.     7:00     A.M.    NA                  NA                 
 7 11:30   P.M.     7:00     A.M.    NA                  NA                 
 8 10:30   P.M.     7:00     A.M.    NA                  NA                 
 9 10:30   P.M.     6:00     A.M.    NA                  NA                 
10 9:30    P.M.     4:30     A.M.    NA                  NA                 
   `Time Difference`
   <drtn>           
 1 NA hours         
 2 NA hours         
 3 NA hours         
 4 NA hours         
 5 NA hours         
 6 NA hours         
 7 NA hours         
 8 NA hours         
 9 NA hours         
10 NA hours         
# … with 148 more rows

Any suggestions?

Cheers,
Jason

There is an error generated when you ran the code block. Can you try resolving that first?

1 Like

I feel somewhat silly. I had undocked the the Rmd file when I ran the code. As soon as I docked the file, the problem went away. This isn't a behavior I would have expected, but I can live with it for now.
Sooooo, your solution worked after all!

Thank you!

Cheers,
Jason

2 Likes

@siddharthprabhu
Ok, although the solution did work earlier, I am now having a different issue and I'm not sure why. I’ve restarted RStudio several times and cannot identify the issues.
As always, any help is greatly appreciated.

dat.psqi.time <- dat.psqi.time %>%
    mutate(Sleep = if_else(bt.sleep == "PM",
                           true = as.POSIXct(x = paste(bedtime, bt.sleep), 
                                          format = "%I:%M %p"), # time in current date
                           false = as.POSIXct(x = paste(bedtime, bt.sleep),
                                           format = "%I:%M %p") + 
                            as.difftime(tim = 1, 
                                        units = "days")), # consider next day for times at/after midnight
           Wake = as.POSIXct(x = paste(waketime, wt.wake),
                          format = "%I:%M %p") + 
            as.difftime(tim = 1, units = "days"), # time in current date plus one day, equivalently time in next day
           `Time Difference` = difftime(time1 = Wake,
                                     time2 = Sleep,
                                     units = "hours")) %>%  # difference of the two times, guaranteed to be in [0, 24)
    print(width = Inf)
#> Error in dat.psqi.time %>% mutate(Sleep = if_else(bt.sleep == "PM", true = as.POSIXct(x = paste(bedtime, : could not find function "%>%"

Created on 2020-04-07 by the reprex package (v0.3.0)

Needs

library(dplyr)

which loads magrittr

The code got as far as the first %>% before choking, and that comes from the second package via the first. The first is needed for the next function, mutate.

Thanks for the suggestion. Unfortunately, that didn’t seem to help and I don't know why.

dat.psqi$bedtime  <- dat.psqi$psqi_1
#> Error in eval(expr, envir, enclos): object 'dat.psqi' not found
dat.psqi$bt.sleep <- dat.psqi$psqi_1a.factor
#> Error in eval(expr, envir, enclos): object 'dat.psqi' not found
dat.psqi$waketime <- dat.psqi$psqi_3
#> Error in eval(expr, envir, enclos): object 'dat.psqi' not found
dat.psqi$wt.wake <- dat.psqi$psqi_3a.factor
#> Error in eval(expr, envir, enclos): object 'dat.psqi' not found


#       VERSION #2 FROM THE FORUMS
library(dplyr, warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 3.6.3

dat.psqi <- dat.psqi %>%
    mutate(Sleep = if_else(bt.sleep == "PM",
                           true = as.POSIXct(x = paste(bedtime, bt.sleep), 
                                          format = "%I:%M %p"), # time in current date
                           false = as.POSIXct(x = paste(bedtime, bt.sleep),
                                           format = "%I:%M %p") + 
                            as.difftime(tim = 1, 
                                        units = "days")), # consider next day for times at/after midnight
           Wake = as.POSIXct(x = paste(waketime, wt.wake),
                          format = "%I:%M %p") + 
            as.difftime(tim = 1, units = "days"), # time in current date plus one day, equivalently time in next day
           `Time Difference` = difftime(time1 = Wake,
                                     time2 = Sleep,
                                     units = "hours")) %>%  # difference of the two times, guaranteed to be in [0, 24)
    print(width = Inf)
#> Error in eval(lhs, parent, parent): object 'dat.psqi' not found

Created on 2020-04-08 by the reprex package (v0.3.0)

Relevant Console output

 bedtime bt.sleep waketime wt.wake Sleep              
   <labelled>      <fct>   <fct>    <fct>    <fct>   <dttm>             
 1 0               10:00   P.M.     5:00     A.M.    NA                 
 2 0               11:00   P.M.     6:30     A.M.    NA                 
 3 1               10:30   P.M.     7:00     A.M.    NA                 
 4 1               11:00   P.M.     7:00     A.M.    NA                 
 5 1               10:00   P.M.     7:30     A.M.    NA                 
 6 0               10:30   P.M.     7:00     A.M.    NA                 
 7 0               11:30   P.M.     7:00     A.M.    NA                 
 8 1               10:30   P.M.     7:00     A.M.    NA                 
 9 0               10:30   P.M.     6:00     A.M.    NA                 
10 1               9:30    P.M.     4:30     A.M.    NA                 
   Wake                `Time Difference`
   <dttm>              <drtn>           
 1 NA                  NA hours         
 2 NA                  NA hours         
 3 NA                  NA hours         
 4 NA                  NA hours         
 5 NA                  NA hours         
 6 NA                  NA hours         
 7 NA                  NA hours         
 8 NA                  NA hours         
 9 NA                  NA hours         
10 NA                  NA hours         
# … with 148 more rows

Does this appear with

ls()

Thanks for the followup.

From the console

ls()
[1] "dat.psqi" "dat1" "data"

Another thing I don't understand is why the the original code from Yarnabrina still works.

Clues, suggestions, advice?
Jason

1 Like

One further clue is left-hand expression aka LHE.

Try testing with a simplified mutate

dat.psqi %>%
    mutate(Sleep = ifelse(bt.sleep == "PM""foo,bar")) -> test_object

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.