简体   繁体   English

如何根据 R 中的不同范围重新分配列的值?

[英]How do I reassign the values of a column based on different ranges in R?

I am working on a sleep measure PSQI now.我现在正在研究睡眠测量 PSQI。 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 的变量是关于每个参与者晚上睡多少小时,其值可以是 6、7.5、8、10、5、9 等。我想创建一个新列,其值更改为不同的值根据不同的范围。 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).睡眠时间低于 5 小时改为 3,睡眠时间 5~6 小时改为 2,6~7 小时改为 1,超过 7 小时改为 0(我想保留睡眠时间的旧栏,他们睡了多少小时晚上)。 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.我知道 mutate 是创建一个新变量,但我不知道如何在括号中设置 arguments。 Could anyone show me how to do that?谁能告诉我该怎么做? Thank you in advance!先感谢您!

ID. ID。 sleep duration.睡眠时间。 PSQI value PSQI 值

a.一个。 6.5 1 6.5 1

b 5 2 b 5 2

c. c。 7.5 0 7.5 0

d. d。 8 0 8 0

e. e. 5 2 5 2

f. F。 9 0 9 0

g. G。 10 0 10 0

h. H。 6 1 6 1

How can I code to get the PSQI value?如何编码以获得 PSQI 值?

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我为新的 R 用户选择了这种方法以提高可读性

We could use case_when from dplyr package:我们可以使用来自case_when dplyr的 case_when:

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

You can also use the dplyr package with: ''Mutate'' to create a new column and ''case_when'' to put the arguments.您还可以使用 dplyr package 与:''Mutate'' 来创建一个新列和 ''case_when'' 来放置 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
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM