Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.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 如何从2个二进制变量创建3个级别的新分类变量?_R - Fatal编程技术网

R 如何从2个二进制变量创建3个级别的新分类变量?

R 如何从2个二进制变量创建3个级别的新分类变量?,r,R,到一个新的分类变量,3个级别0=不吸烟,1=过去吸烟,2=当前吸烟? 谢谢你的帮助 我对R编程非常陌生我们可以使用case\u将分类为新类别,即当“过去吸烟”为1时返回1,当当前吸烟为1时返回2,否则返回0 EC <- data.frame(id=c(1,2,3,4,5,6,7,8,9), past_smoking=c("1","0","0","0","1","0",&q

到一个新的分类变量,3个级别0=不吸烟,1=过去吸烟,2=当前吸烟? 谢谢你的帮助 我对R编程非常陌生

我们可以使用case\u将分类为新类别,即当“过去吸烟”为1时返回1,当当前吸烟为1时返回2,否则返回0

EC <- data.frame(id=c(1,2,3,4,5,6,7,8,9), past_smoking=c("1","0","0","0","1","0","0","0","0"),current_smoking=c("0","1","1","1","0","1","1","1","0"))

EC

  id past_smoking current_smoking
1  1            1               0
2  2            0               1
3  3            0               1
4  4            0               1
5  5            1               0
6  6            0               1
7  7            0               1
8  8            0               1
9  9            0               0
或者如@user20650所述

library(dplyr)
EC %>%
   mutate(categ = case_when(past_smoking =='1' ~ '1',
         current_smoking == '1' ~ '2', TRUE ~ '0'))
我们可以使用case_将分类为新类别,即当“过去_吸烟”为1时返回1,当当前_吸烟为1时返回2,否则为“0”

EC <- data.frame(id=c(1,2,3,4,5,6,7,8,9), past_smoking=c("1","0","0","0","1","0","0","0","0"),current_smoking=c("0","1","1","1","0","1","1","1","0"))

EC

  id past_smoking current_smoking
1  1            1               0
2  2            0               1
3  3            0               1
4  4            0               1
5  5            1               0
6  6            0               1
7  7            0               1
8  8            0               1
9  9            0               0
或者如@user20650所述

library(dplyr)
EC %>%
   mutate(categ = case_when(past_smoking =='1' ~ '1',
         current_smoking == '1' ~ '2', TRUE ~ '0'))

也许你可以试试下面的代码

# your data
EC <- data.frame(id=c(1,2,3,4,5,6,7,8,9), past_smoking=c("1","0","0","0","1","0","0","0","0"),current_smoking=c("0","1","1","1","0","1","1","1","0"))

# create new column
EC_new <- EC %>% 
  mutate(newvar=case_when(past_smoking==0 & current_smoking==0 ~ 0,
                          past_smoking==1 ~ 1,
                          current_smoking==1 ~ 2))

# make a factor variable
EC_new$newvar <- factor(EC_new$newvar, levels = c(0,1,2),
         labels = c("no_smoking", "past_smoking", "current_smoking"))

transform(
  type.convert(EC, as.is = TRUE),
  newvar = past_smoking + 2 * current_smoking
)

也许你可以试试下面的代码

# your data
EC <- data.frame(id=c(1,2,3,4,5,6,7,8,9), past_smoking=c("1","0","0","0","1","0","0","0","0"),current_smoking=c("0","1","1","1","0","1","1","1","0"))

# create new column
EC_new <- EC %>% 
  mutate(newvar=case_when(past_smoking==0 & current_smoking==0 ~ 0,
                          past_smoking==1 ~ 1,
                          current_smoking==1 ~ 2))

# make a factor variable
EC_new$newvar <- factor(EC_new$newvar, levels = c(0,1,2),
         labels = c("no_smoking", "past_smoking", "current_smoking"))

transform(
  type.convert(EC, as.is = TRUE),
  newvar = past_smoking + 2 * current_smoking
)

丽莎请考虑检查丽莎请考虑检查。