0

I am working on a sleep measure PSQI now. A variable called sleep duration is about how many hours each participant sleeps at night, the value of which can be 6, 7.5, 8, 10, 5, 9, etc. I want to create a NEW column which the values changed to different values based on different ranges. Sleep duration under 5 hours is changed to 3, sleep duration 5~6 hours to 2, 6~7 hours to 1, and longer than 7 hours to 0 (I want to keep the old column of sleep duration with how many hours they sleep at night). I have been looking for information regarding the script, but cannot find exactly what I want to do. I know mutate is to create a new variable, but I don't know how to set the arguments in the parentheses. Could anyone show me how to do that? Thank you in advance!

ID. sleep duration. PSQI value

a. 6.5 1

b 5 2

c. 7.5 0

d. 8 0

e. 5 2

f. 9 0

g. 10 0

h. 6 1

How can I code to get the PSQI value?

3 Answers3

2
NewCol <- OldCol
NewCol[OldCol<5] <- 3
NewCol[OldCol>=5 & OldCol<6] <- 2
NewCol[OldCol>=6 & OldCol<7] <- 1
NewCol[OldCol>=7] <- 0
df <- data.frame(OldCol,NewCol)

I chose this method for readability for a new R user

1

We could use case_when from dplyr package:

library(dplyr)
df %>%  
  mutate(NEW = case_when(sleep_duration < 5 ~ 3,
                         sleep_duration >=5 & sleep_duration < 6 ~ 2,
                         sleep_duration >=6 & sleep_duration < 7 ~ 1,
                         sleep_duration >=7 ~ 0))

Output:

  sleep_duration NEW
1            6.0   1
2            7.5   0
3            8.0   0
4           10.0   0
5            5.0   2
6            9.0   0

data:

df <- data.frame(sleep_duration = c(6, 7.5, 8, 10, 5, 9))
TarJae
  • 72,363
  • 6
  • 19
  • 66
1

You can also use the dplyr package with : ''Mutate'' to create a new column and ''case_when'' to put the arguments.

 librarby(dplr)
NewData = mutate(OldData, Newvariable = case_when(
  OldVariable < 5 ~ 3,
  OldVariable >= 5 & OldVariable <5  ~ 2,
  OldVariable >= 6 & OldVariable <7  ~ 1,
  OldVariable >= 7 ~ 0
))