我有一个大型 DHS 数据集,我在下面附上了其中的一个小样本。我正在使用 mutate 和 case_when 为变量赋值并设置标签。但是,当我运行代码时,我得到了,我也共享下面的代码和数据集
#This is my sample datset
structure(list(v000 = structure(c("BJ7", "BJ7", "BJ7", "BJ7",
"BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7",
"BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7",
"BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7",
"BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7", "BJ7"
), label = "Country code and phase"), v411 = structure(c(0, 1,
1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0), label = "Gave child tinned, powdered or fresh milk"),
v411a = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 1, 0, 1, 1, 0), label = "Gave child baby formula"),
v412 = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child fresh milk"), v412a = structure(c(0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1,
0), label = "Gave child fortified baby food (cerelac, etc)"),
v412b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child other porridge/gruel"), v412c = structure(c(0,
1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1), label = "Gave child soup/clear broth"), v413 = structure(c(0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1,
0), label = "Gave child other liquid"), v413a = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "NA - Gave child CS liquid"),
v413b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child CS liquid"), v413c = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "NA - Gave child CS liquid"),
v413d = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child CS liquid"), v414a = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "NA - Gave child CS foods"),
v414b = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child CS foods"), v414c = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), label = "NA - Gave child CS foods"),
v414d = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child CS foods"), v414e = structure(c(0,
1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0), label = "Gave child bread, noodles, other made from grains"),
v414f = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0), label = "Gave child potatoes, cassava, or other tubers"),
v414g = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 8, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 1, 0, 1, 1, 0), label = "Gave child eggs"), v414h = structure(c(0,
1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0), label = "Gave child meat (beef, pork, lamb, chicken, etc)"),
v414i = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 1, 0, 0, 1, 0), label = "Gave child pumpkin, carrots, squash (yellow or orange inside)"),
v414j = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 1, 1, 0), label = "Gave child any dark green leafy vegetables"),
v414k = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0), label = "Gave child mangoes, papayas, other vitamin A fruits"),
v414l = structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 1, 0), label = "Gave child any other fruits"),
v414m = structure(c(0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 0, 1, 0, 0), label = "Gave child liver, heart, other organs"),
v414n = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 8, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 1, 0, 0, 0), label = "Gave child fish or shellfish"),
v414o = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0), label = "Gave child food made from beans, peas, lentils, nuts"),
v414p = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1,
0, 0, 0, 1, 0, 1, 0, 0), label = "Gave child cheese, yogurt, other milk products"),
v414q = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 8, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 1, 1, 0, 0), label = "Gave child oil, fats, butter, products made of them"),
v414r = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), label = "NA - Gave child chocolates, sweets, candies, pastries, etc"),
v414s = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 8, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 8, 0, 0, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0), label = "Gave child other solid-semisolid food"),
v414t = structure(c(0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 0, 0, 1, 0), label = "Gave child CS foods")), row.names = c(NA,
-40L), label = "bjkr71fl dataset written by Stat/Transfer Ver. 13.2.801.0615", class = c("tbl_df",
"tbl", "data.frame"))
#This is my code
# *** Foods consumed ***
KRiycf <- KRiycf %>%
# country specific foods. These can be added to the foods below based on the survey. See example for nt_root & nt_meatfish below
mutate(food1 = case_when(v414a==1 ~ 1 , v414a!=1 ~ 0)) %>%
mutate(food2 = case_when(v414b==1 ~ 1 , v414a!=1 ~ 0)) %>%
mutate(food3 = case_when(v414c==1 ~ 1 , v414a!=1 ~ 0)) %>%
mutate(food4 = case_when(v414d==1 ~ 1 , v414a!=1 ~ 0)) %>%
mutate(nt_formula = case_when(v411a==1 ~ 1 , v411a!=1~ 0)) %>% # Given formula
mutate(nt_milk = case_when(v411==1 ~ 1 , v411!=1~ 0)) %>% # Given other milk
mutate(nt_liquids= case_when(v410==1 | v412c==1 | v413==1 ~ 1 , v410!=1 | v412c!=1 | v413!=1 ~ 0)) %>% # Given other liquids
mutate(nt_bbyfood = case_when(v412a==1 ~ 1 , v412a!=1~ 0)) %>% # Given fortified baby food
mutate(nt_grains = case_when(v412a==1 | v414e==1 ~ 1 , v412a!=1 | v414e!=1 ~ 0)) %>% # Given grains
mutate(nt_vita = case_when(v414i==1 | v414j==1 | v414k==1 ~ 1 , v414i!=1 | v414j!=1 | v414k!=1 ~ 0)) %>% # Given Vit A rich foods
mutate(nt_frtveg = case_when(v414l==1 ~ 1 , v414l!=1~ 0)) %>% # Given other fruits or vegetables
mutate(nt_root = case_when((v000 == "UG7" & (v414f==1 | food1==1)) | (v000 != "UG7" & v414f==1) ~ 1,
(v000 == "UG7" & (v414f!=1 | food1!=1)) | (v000 != "UG7" & v414f!=1) ~ 0)) %>% #given roots and tubers
mutate(nt_nuts = case_when(v414o==1 ~ 1 , v414o!=1~ 0)) %>% # Given nuts or legumes
mutate(nt_meat = case_when((v000 == "UG7" & (v414h==1 |v414m==1 | food2==1)) | (v000 != "UG7" & (v414h==1 | v414m==1)) ~ -1,
(v000 == "UG7" & !(v414h==1 |v414m==1 | food2==1)) | (v000 != "UG7" & !(v414h==1 | v414m==1)) ~ 0)) %>% #given meat
mutate(nt_fish = case_when(v000== "UG7" & (v414n ==1 | food2==1)) | (v000 != "UG7" & (v414n =1)) ~1,
(v000 = "UG7" & !(v414n==1 | food2==1)) | (v000!="UG7" & !(v414n==1))~0) %>% # Given fish
mutate(nt_eggs = case_when(v414g==1 ~ 1 , v414g!=1~ 0)) %>% #given eggs
mutate(nt_dairy = case_when(v414p==1 | v414v==1 ~ 1 , v414p!=1 | v414v!=1 ~ 0)) %>% # Given dairy
mutate(nt_solids = case_when( nt_bbyfood==1 | nt_grains==1 | nt_vita==1 | nt_frtveg==1 | nt_root==1 | nt_nuts==1 | nt_meatfish==1 |
nt_eggs==1 | nt_dairy==1 | v414s==1 ~ 1 ,
nt_bbyfood!=1 | nt_grains!=1 | nt_vita!=1 | nt_frtveg!=1 | nt_root!=1 | nt_nuts!=1 | nt_meatfish!=1 |
nt_eggs!=1 | nt_dairy!=1 | v414s!=1 ~ 0) ) %>%
#add labels
set_value_labels(nt_formula = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_formula = "Child given infant formula in day/night before survey - last-born under 2 years") %>%
set_value_labels(nt_milk = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_milk = "Child given other milk in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_liquids = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_liquids = "Child given other liquids in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_bbyfood = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_bbyfood = "Child given fortified baby food in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_grains = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_grains = "Child given grains in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_vita = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_vita = "Child given vitamin A rich food in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_frtveg = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_frtveg = "Child given other fruits or vegetables in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_root = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_root = "Child given roots or tubers in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_nuts = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_nuts = "Child given legumes or nuts in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_meatfish = c("Yes" = -1, "No"=0 )) %>%
set_variable_labels(nt_meatfish = "Child given meat, fish, shellfish, or poultry in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_fish = C("Yes" = 1, "No" = 0))%>%
set_variable_labels(nt_fish = "Child given fish or shellfish in day/night before survey- last-born under 2 years")%>%
set_value_labels(nt_eggs = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_eggs = "Child given eggs in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_dairy = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_dairy = "Child given cheese, yogurt, or other milk products in day/night before survey- last-born under 2 years") %>%
set_value_labels(nt_solids = c("Yes" = 1, "No"=0 )) %>%
set_variable_labels(nt_solids = "Child given any solid or semisolid food in day/night before survey- last-born under 2 years")
运行代码时出现以下错误。
mutate()
中的错误:
ℹ 在争论中:nt_fish = ~...
。
错误原因:
! nt_fish
必须是向量,而不是 <named list>
)
mutate(., nt_fish = case_when(v000 == "UG7" & (v414n == 1 | food2 == :错误导致: !
nt_fish
必须是向量,而不是 我正在尝试从数据集中改变变量 v414n,如果答案是肯定的,我想为其分配值 1,如果答案是否定的,我想为其分配值 0。如果鱼被吃掉,我想将变量重命名为 nt_fish,如果为 1,则将值标签设置为“是”;如果为否,则将值标签设置为 0。
我尝试在所有变量上使用 mutate 和 case_when ,但它对 nt_fish 不起作用,它适用于其余变量。
你的 case_when 调用时有一个额外的括号,就在第一个
food2==1
之后:
mutate(nt_fish = case_when(
v000== "UG7" & (v414n ==1 | food2==1)) | (v000 != "UG7" & (v414n =1)) ~1,
(v000 = "UG7" & !(v414n==1 | food2==1)) | (v000!="UG7" & !(v414n==1))~0) %>% # Given fish
应该是
mutate(nt_fish = case_when(
v000== "UG7" & (v414n ==1 | food2==1) | (v000 != "UG7" & (v414n =1)) ~1,
(v000 = "UG7" & !(v414n==1 | food2==1)) | (v000!="UG7" & !(v414n==1))~0) %>% # Given fish