Skip to content
Prev 386145 / 398513 Next

Error: variable not found

Hi Hannah,
Using the same code I sent before, you can append the partner codes to the
household code. I apologize, but I don't know how to use the
dplyr/tidyr/... stuff so this is written in straight R code using logic
statements.

ipumsi_00008_dta<-
 read.table(
 text="country year sample serial hhwt pernum perwt resident sploc sprule
204 2013 204201301 4000 10 1 10 1 5 2
204 2013 204201301 4000 10 2 10 1 0 2
204 2013 204201301 4000 10 3 10 1 0 2
204 2013 204201301 4000 10 4 10 1 0 2
204 2013 204201301 4000 10 5 10 1 1 2
204 2013 204201301 4000 10 6 10 1 1 2
204 2013 204201301 4000 10 7 10 1 0 2
204 2013 204201301 4000 10 8 10 1 0 2
204 2013 204201301 4000 10 9 10 1 0 2
204 2013 204201301 7000 10 1 10 1 2 1
204 2013 204201301 7000 10 2 10 1 1 1
204 2013 204201301 7000 10 3 10 1 0 0
204 2013 204201301 7000 10 4 10 1 5 1
204 2013 204201301 7000 10 5 10 1 4 1",
header=TRUE,stringsAsFactors=FALSE)
for(hh in unique(ipumsi_00008_dta$serial)) {
 cat("hh",hh," ")
 for(ind in ipumsi_00008_dta$pernum[ipumsi_00008_dta$serial == hh]) {
  cat("ind",ind,"\n")
  if(ipumsi_00008_dta$sploc[ipumsi_00008_dta$serial == hh &
    ipumsi_00008_dta$pernum == ind] > 0) {
   cat("sploc > 0\n")
   relationships<-
    ipumsi_00008_dta$pernum[ipumsi_00008_dta$serial == hh &
     ipumsi_00008_dta$sploc == ind]
   cat(relationships,"\n")
   ipumsi_00008_dta$sprule[ipumsi_00008_dta$serial == hh &
    ipumsi_00008_dta$pernum == ind]<-
     paste(c(hh,relationships),sep="",collapse="_")
  } else {
   ipumsi_00008_dta$sprule[ipumsi_00008_dta$serial == hh &
    ipumsi_00008_dta$pernum == ind]<-hh
  }
 }
}
ipumsi_00008_dta

This appends the partner codes using "_" as a separator. You can do it
without the "_" and get a numeric variable, but I think you will generate
ambiguous "sprule" codes. Again, this will not work with between-household
relationships.

Jim

On Fri, Oct 30, 2020 at 11:16 PM Hannah Van Impe <hannahvanimpe at outlook.com>
wrote:

  
  
Message-ID: <CA+8X3fWApQ512E8hJUwnYA+apjJAP_DTicVpMx5CGruWrcHbqA@mail.gmail.com>
In-Reply-To: <B5A988CB-7B76-4CDA-9397-F66CABDBA1A2@outlook.com>