[R] Error: variable not found

Michael Dewey ||@t@ @end|ng |rom dewey@myzen@co@uk
Fri Oct 30 12:43:54 CET 2020


Dear Hannah

I think the problem is that attach() is not doing what you think it is. 
It does seem to make it easy to make mistakes. I would suggest switching 
to using with() instead or using the data = parameter to functions which 
support it.

Michael

On 30/10/2020 08:15, Hannah Van Impe wrote:
> Hello
> 
> I have a question. I made an r-script and did a few commands needed to make some new variables. They all work out well, and when I run the commands, the new variables appear in the dataset. I can also work with these new variables to make other new variables from them. Also, when I use summary(dataset), those new variables appear in the summary of the dataset.
> But, when I do summary(new variable) => error: variable not found.
> For example: summary(couple_id) => Error in summary(couple_id) : object 'couple_id' not found. What can I do about this?
> 
> R-script:
> attach(ipumsi_00008_dta)
> library(tinytex)
> library(dplyr)
> library(ggplot2)
> library(tidyr)
> library(knitr)
> library(forcats)
> library(mice)
> library(pander)
> library(ggcorrplot)
> library(lubridate)
> # true/false code when sploc is greater than zero and sprule is equal to 1 or 2
> ipumsi_00008_dta <- mutate(ipumsi_00008_dta, rule_union = sploc>0 & (sprule==1 | sprule==2))
> ## creating numeric code for rule_union & rule_unionn: 1 when sploc is greater than zero and sprule is equal to 1 or 2, 0 if not.
> ## This is neccesary because otherwise it is a logical code and we cannot multiply with it, which is needed
> ipumsi_00008_dta <- ipumsi_00008_dta %>% mutate(rule_union_numeric = as.numeric(rule_union))
> ### creating unique numeric code for sploc / pernum variables
> ipumsi_00008_dta <- mutate(ipumsi_00008_dta, sploc_pernum_code = pernum*(sex==1) + sploc*(sex==2))
> #### dividing serial by 1000, otherwise, the ultimate couple_id is too large, and it works in this dataset because the serials start at 1000 (I will have to check if this works for other datasets)
> ipumsi_00008_dta <- mutate(ipumsi_00008_dta, serial_divided = serial%/%1000)
> ##### creating unique union identifier
> ipumsi_00008_dta$union_id = paste0(ipumsi_00008_dta$country, ipumsi_00008_dta$year, ipumsi_00008_dta$serial_divided, ipumsi_00008_dta$sploc_pernum_code)
> ipumsi_00008_dta <- ipumsi_00008_dta %>% mutate(union_id_numeric = as.numeric(union_id))
> ipumsi_00008_dta <- mutate(ipumsi_00008_dta, couple_id = union_id_numeric*rule_union_numeric)
> ###### Understanding the couple_id variable
> summary(couple_id)
> summary(ipumsi_00008_dta)
> 
> I also attached my dataset.
> 
> Thank you very much for the answer!
> Hannah Van Impe
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Michael
http://www.dewey.myzen.co.uk/home.html



More information about the R-help mailing list