[R] Error: variable not found
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Sat Oct 31 00:36:01 CET 2020
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 using outlook.com>
wrote:
> Thank you very much for the answer.
>
> I also have another question. With this data, I made the variable union_id
> using paste0. (I am writing a thesis and this part is necessary, but I
> don’t have previous knowledge of R, so it is difficult for me). My
> professor told me, that if I use paste0, it can be problematic because
> ‘pernum’ and ‘serial’ can have different numbers of digits. In other words,
> if pernum is 12 then paste0 prints 12, but if pernum is 1 paste0 prints 1
> instead of 01, so you will not have that the last two digits always
> correspond to pernum. He suggest me to use an alternative way for creating
> this union_id variable. Do you have any idea how I can do this?
>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list