Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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 在以下情况下创建带有case_的组标识变量_R_Dplyr_Data Manipulation_Case When - Fatal编程技术网

R 在以下情况下创建带有case_的组标识变量

R 在以下情况下创建带有case_的组标识变量,r,dplyr,data-manipulation,case-when,R,Dplyr,Data Manipulation,Case When,我有一个属于大家庭的个人数据集,因此有一个变量可以确定受访者与被采访的户主(父母、孩子、兄弟等)的关系 我想创建一个变量来标识他们的“生成组”。 我的小组是: gen0 <- c("grandparent", "grandparent_ofwife") gen1 <- c("parent", "parent_inlaw", "parent_ofcohab") gen2 <- c("head", "wife_legal", "wife_cohabit", "husband_leg

我有一个属于大家庭的个人数据集,因此有一个变量可以确定受访者与被采访的户主(父母、孩子、兄弟等)的关系

我想创建一个变量来标识他们的“生成组”。 我的小组是:

gen0 <- c("grandparent", "grandparent_ofwife")
gen1 <- c("parent", "parent_inlaw", "parent_ofcohab")
gen2 <- c("head", "wife_legal", "wife_cohabit", "husband_legal", "y1_cohab")
gen3 <- c("child", "child_step", "child_ofwife", "child_inlaw", "child_foster", "child_1y_cohab")

这很有效。我认为主要的问题是变量名周围的引号。但是,列的名称也不能以数字开头

gen1 <- c("parent", "parent_inlaw", "parent_ofcohab")
gen2 <- c("head", "wife_legal", "wife_cohabit", "husband_legal", "y1_cohab")
gen3 <- c("child", "child_step", "child_ofwife", "child_inlaw", "child_foster", "child_1y_cohab")
library(dplyr)
dat <- data.frame("x2017_relation_head" = sample(c(gen0, gen1, gen2, gen3),
                                                size = 100, replace = TRUE))
dat$x2017_relation_head <- as.character(dat$x2017_relation_head)
dat2<- dat %>% mutate(genx = 
          case_when(x2017_relation_head %in% gen0 ~ "gen0",
            x2017_relation_head %in% gen1 ~ "gen1",
            x2017_relation_head %in% gen2 ~ "gen2",
            x2017_relation_head %in% gen3 ~ "gen3"))
head(dat2)
  x2017_relation_head genx
1      child_1y_cohab gen3
2         child_inlaw gen3
3          child_step gen3
4       husband_legal gen2
5          child_step gen3
6         child_inlaw gen3

gen1而不是单引号(
)使用反引号引用列名。(
`
)。啊,是的,就是这样,我犯了一个愚蠢的错误!非常感谢。
id  2017_relation_head
1   wife_legal
2   head
3   wife_legal
4   head
5   wife_legal
6   head
7   wife_legal
8   child
9   child
10  NA
11  child
12  child
13  child
14  child
15  child
16  head
17  parent
18  NA
19  grandchild
20  child_step
gen1 <- c("parent", "parent_inlaw", "parent_ofcohab")
gen2 <- c("head", "wife_legal", "wife_cohabit", "husband_legal", "y1_cohab")
gen3 <- c("child", "child_step", "child_ofwife", "child_inlaw", "child_foster", "child_1y_cohab")
library(dplyr)
dat <- data.frame("x2017_relation_head" = sample(c(gen0, gen1, gen2, gen3),
                                                size = 100, replace = TRUE))
dat$x2017_relation_head <- as.character(dat$x2017_relation_head)
dat2<- dat %>% mutate(genx = 
          case_when(x2017_relation_head %in% gen0 ~ "gen0",
            x2017_relation_head %in% gen1 ~ "gen1",
            x2017_relation_head %in% gen2 ~ "gen2",
            x2017_relation_head %in% gen3 ~ "gen3"))
head(dat2)
  x2017_relation_head genx
1      child_1y_cohab gen3
2         child_inlaw gen3
3          child_step gen3
4       husband_legal gen2
5          child_step gen3
6         child_inlaw gen3