R

R,r,join,error-handling,R,Join,Error Handling,我使用dplyr包的full\u-join功能加入了五个数据集。第一个数据集有6165行;第二个数据集有5827行。最终联接的数据集有33503行。 我使用以下代码来连接这五个数据集 n2<-full_join(n96, n01) n3<-full_join(n2, n06) n4<-full_join(n3, n11) nf<-full_join(n4, n16) View(nf) n2我不确定update函数是如何工作的,但您似乎想更

我使用
dplyr
包的
full\u-join
功能加入了五个数据集。第一个数据集有6165行;第二个数据集有5827行。最终联接的数据集有33503行。 我使用以下代码来连接这五个数据集

n2<-full_join(n96, n01)
    n3<-full_join(n2, n06)
    n4<-full_join(n3, n11)
    nf<-full_join(n4, n16)
    View(nf)

n2我不确定
update
函数是如何工作的,但您似乎想更改变量的因子级别。在将数据帧传递给
svydesign
函数之前,可以在
nf
数据帧中执行此操作

library(dplyr)
nf <- nf %>%
  mutate(edu = factor( education, levels = c(0, 1, 2, 3), labels = 
                c("no edu", "primary", "secondary", "higher") ),
        wealth =factor( wealth, levels = c(1, 2, 3, 4, 5) , labels = 
                  c("poorest", "poorer", "middle", "richer", "richest")),
        marital = factor( marital, levels = c(0, 1) , labels = 
                    c( "never married", "married")),
        occu = factor( occu, levels = c(0, 1, 2, 3) , labels =
                 c( "not working" , "professional/technical/manageral/clerial/sale/services" , "agricultural", "skilled/unskilled manual") ),
        age1 = factor(age1, levels = c(1, 2, 3), labels =
                c( "early" , "mid", "late") ),
        obov= factor(obov, levels = c(0, 1, 2), labels= 
               c("normal", "overweight", "obese")),
        over= factor(over, levels = c(0, 1), labels= 
               c("normal", "overweight/obese")),
       working_status= factor (working_status, levels = c(0, 1), labels = c("not working", "working")),
      education1= factor (education1, levels = c(0, 1, 2), labels= 
                      c("no education", "primary", "secondary/secondry+")),
      resi= factor (resi, levels= c(0,1), labels= c("urban", "rural")))
库(dplyr)
nf%
变异(教育程度=因素(教育程度,等级=c(0,1,2,3),标签=
("无教育程度","小学","中学","高级"),,
财富=系数(财富,等级=c(1,2,3,4,5),标签=
c(“最穷”、“较穷”、“中等”、“较富”、“最富有”),
婚姻=因素(婚姻,等级=c(0,1),标签=
c(“未婚”、“已婚”),
占用=系数(占用,级别=c(0,1,2,3),标签=
c(“不工作”、“专业/技术/管理/文书/销售/服务”、“农业”、“熟练/非熟练手册”),
年龄1=系数(年龄1,级别=c(1,2,3),标签=
("早,中,晚),,
obov=系数(obov,级别=c(0,1,2),标签=
c(“正常”、“超重”、“肥胖”),
over=系数(over,levels=c(0,1),标签=
c(“正常”、“超重/肥胖”),
工作状态=系数(工作状态,级别=c(0,1),标签=c(“不工作”,“工作”),
教育1=系数(教育1,级别=c(0,1,2),标签=
c(“无教育”、“小学”、“中学/中学+”),
resi=系数(resi,等级=c(0,1),标签=c(“城市”、“农村”))
svs<-svydesign(id=nf$v021, strata=nf$stra, nest=TRUE, weights=nf$wgtv, data=nf)
svs1 <- 
  update(
    svs, 
    one=1, 
    edu = factor( education, levels = c(0, 1, 2, 3), labels = 
                    c("no edu", "primary", "secondary", "higher") ),
    
    wealth =factor( wealth, levels = c(1, 2, 3, 4, 5) , labels = 
                      c("poorest", "poorer", "middle", "richer", "richest")),
    marital = factor( marital, levels = c(0, 1) , labels = 
                        c( "never married", "married")),
    occu = factor( occu, levels = c(0, 1, 2, 3) , labels =
                           c( "not working" , "professional/technical/manageral/clerial/sale/services" , "agricultural", "skilled/unskilled manual") ),
    age1 = factor(age1, levels = c(1, 2, 3), labels =
                   c( "early" , "mid", "late") ),
    obov= factor(obov, levels = c(0, 1, 2), labels= 
                      c("normal", "overweight", "obese")),
    
    over= factor(over, levels = c(0, 1), labels= 
                   c("normal", "overweight/obese")),
    
    working_status= factor (working_status, levels = c(0, 1), labels = c("not working", "working")),
    education1= factor (education1, levels = c(0, 1, 2), labels= 
                          c("no education", "primary", "secondary/secondry+")),
    resi= factor (resi, levels= c(0,1), labels= c("urban", "rural"))
  )
Error in `[<-.data.frame`(`*tmp*`, , newnames[j], value = c(3L, 3L, 3L,  : 
  replacement has 12674 rows, data has 33503
library(dplyr)
nf <- nf %>%
  mutate(edu = factor( education, levels = c(0, 1, 2, 3), labels = 
                c("no edu", "primary", "secondary", "higher") ),
        wealth =factor( wealth, levels = c(1, 2, 3, 4, 5) , labels = 
                  c("poorest", "poorer", "middle", "richer", "richest")),
        marital = factor( marital, levels = c(0, 1) , labels = 
                    c( "never married", "married")),
        occu = factor( occu, levels = c(0, 1, 2, 3) , labels =
                 c( "not working" , "professional/technical/manageral/clerial/sale/services" , "agricultural", "skilled/unskilled manual") ),
        age1 = factor(age1, levels = c(1, 2, 3), labels =
                c( "early" , "mid", "late") ),
        obov= factor(obov, levels = c(0, 1, 2), labels= 
               c("normal", "overweight", "obese")),
        over= factor(over, levels = c(0, 1), labels= 
               c("normal", "overweight/obese")),
       working_status= factor (working_status, levels = c(0, 1), labels = c("not working", "working")),
      education1= factor (education1, levels = c(0, 1, 2), labels= 
                      c("no education", "primary", "secondary/secondry+")),
      resi= factor (resi, levels= c(0,1), labels= c("urban", "rural")))