Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个级别为R的新变量_R - Fatal编程技术网

创建一个级别为R的新变量

创建一个级别为R的新变量,r,R,我是R的新手,目前正在努力学习这门语言。我想做的是创建一个具有三个级别的新变量(如果有意义的话)。我试图表明,嵌套的ifelse语句几乎从来都不是正确的答案。它们很难阅读,而且易碎。您的目标是将连续值(perctchng中的数值)转换为分类值(“级别1”等)。R的cut功能非常适合: pincome <- data.frame(perctchng = c(-2, -1, 0, 1, 2, 5)) perctchng 1 -2 2 -1 3

我是R的新手,目前正在努力学习这门语言。我想做的是创建一个具有三个级别的新变量(如果有意义的话)。我试图表明,嵌套的ifelse语句几乎从来都不是正确的答案。它们很难阅读,而且易碎。您的目标是将连续值(perctchng中的数值)转换为分类值(“级别1”等)。R的
cut
功能非常适合:

pincome <- data.frame(perctchng = c(-2, -1, 0, 1, 2, 5))

  perctchng
1        -2
2        -1
3         0
4         1
5         2
6         5

pincome$level <- cut(pincome$perctchng, c(-Inf, 0, 1, Inf), c('level 1', 'level 2', 'level 3'))

  perctchng   level
1        -2 level 1
2        -1 level 1
3         0 level 1
4         1 level 2
5         2 level 3
6         5 level 3

pincome嵌套的
ifelse
语句几乎从来都不是正确的答案。它们很难阅读,而且易碎。您的目标是将连续值(perctchng
中的数值)转换为分类值(“级别1”等)。R的
cut
功能非常适合:

pincome <- data.frame(perctchng = c(-2, -1, 0, 1, 2, 5))

  perctchng
1        -2
2        -1
3         0
4         1
5         2
6         5

pincome$level <- cut(pincome$perctchng, c(-Inf, 0, 1, Inf), c('level 1', 'level 2', 'level 3'))

  perctchng   level
1        -2 level 1
2        -1 level 1
3         0 level 1
4         1 level 2
5         2 level 3
6         5 level 3

pincome您的解决方案几乎不错您的语法不正确:

library(dplyr)

pincome <- data.frame(perctchng = c(-2, -1, 0, 1, 2, 5))

pincome %>%
  mutate(perctchng_level = ifelse(perctchng <= 0, "level 1",
                            ifelse(perctchng < 1, "level 2",
                                   ifelse(perctchng >= 1, "level 3", NA))))

您的解决方案几乎不错您的语法不正确:

library(dplyr)

pincome <- data.frame(perctchng = c(-2, -1, 0, 1, 2, 5))

pincome %>%
  mutate(perctchng_level = ifelse(perctchng <= 0, "level 1",
                            ifelse(perctchng < 1, "level 2",
                                   ifelse(perctchng >= 1, "level 3", NA))))
tidyverse解决方案:

pincome$perctchngtidyverse解决方案:


pincome$perctchngTry
pincome$perctchngTry的可能副本我发现,如果使用适当的缩进,理解这类嵌套语句(尤其是ifelse语句!)会非常容易。你的想法是对的,你只是在括号顺序上犯了错误。你真的确定你想要Try
pincome$perctchngPossible duplicate of I find如果使用适当的缩进,理解这些嵌套语句(特别是ifelse语句!)会非常容易。你的想法是对的,你只是在括号顺序上犯了错误。你真的确定你想要级别吗?不幸的是,在这里使用
cut
并不能满足OP的要求。默认情况下,
cut
使用左开右闭的间隔,因此,级别是有一个参数来控制此行为的
cut
includeLowest
。不幸的是,在这里使用
cut
并不能满足OP的要求。默认情况下,
cut
使用左打开、右关闭的间隔,因此,级别是有一个参数控制此行为的
cut
library(tidyverse)
pincome <- pincome %>%
           mutate(perctchng = case_when(perctchng <= 0 ~ "level 1",
                                        perctchng < 1 ~ "level 2",
                                        perctchng >= 1 ~ "level 3",
                                        TRUE ~ NA_character_)