[R] Creating unique code

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Thu Oct 29 01:47:10 CET 2020


Hi Hannah,
Yes, that does give me more insight. The polygyny only doesn't matter, for
even if there was polyandry it could be coded in the same way. If I
understand this correctly you have one variable (SPRULE) that must contain
information about the household of the individual and the identities of his
(or her) relationship partner(s). The household number can accommodate up
to three relationship partners as long as there are a maximum of 9 people
per household. Given these constraints, I think this may do what you want:

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")
   if(length(relationships > 1)) {
    ipumsi_00008_dta$sprule[ipumsi_00008_dta$serial == hh &
     ipumsi_00008_dta$pernum == ind]<-
      hh+as.numeric(paste0(relationships,collapse=""))
   } else {
    ipumsi_00008_dta$sprule[ipumsi_00008_dta$serial == hh &
     ipumsi_00008_dta$pernum == ind]<-hh+
    ipumsi_00008_dta$sploc[ipumsi_00008_dta$serial == hh &
     ipumsi_00008_dta$pernum == ind]
   }
  } else {
   ipumsi_00008_dta$sprule[ipumsi_00008_dta$serial == hh &
    ipumsi_00008_dta$pernum == ind]<-hh
  }
 }
}
ipumsi_00008_dta

Note that this is a bad way to create a field in a database and it would be
better to create the information on the fly with a query.
It won't work with between household relationships

Jim

On Wed, Oct 28, 2020 at 10:08 PM Hannah Van Impe <hannahvanimpe using outlook.com>
wrote:

> Hello
> Again, thank you very much for the help!!
>
...

> In this foto, you can see that there is a polygamous union in household 6.
> The men always only shows 1 sploc variable, even when they have multiple
> women. So, only men have multiple women and women don’t have multiple men.
> So here, The man is observation 49, and he is linked with woman 53, but he
> can also be linked with other women. We see that woman 53 is linked with
> this man, but also woman 54 is linked with this man. So the man in
> observation 49 is linked with two women, 53 and 54, and I would like to
> give these 3 observations the same numerical code, so I can identity this
> union.
> Is it possible to give more explanation when you have this information?
> Hannah
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list