0

I'm running a code to format data, which goes into a rake weighting analysis. I started to run the lines but when I get to the parts "tibble::enframe" they don't run. What does tibble do in this code? The strange thing was it did run once upon a time in past but no longer does.

the code

dummy_data_age <- sample(c(18:90), 100, replace = TRUE)
dummy_data_income <- sample(c(0:100000), 100, replace = TRUE)
dummy_data_gender <- sample(c("Male", "Female"), 100, replace = TRUE)
dummy_data_gender <- as.integer(factor(dummy_data_gender))
dummy_data_region <- sample(c("North", "East", "South", "West"), 100, replace = TRUE)
dummy_data_region <- as.integer(factor(dummy_data_region))
dummy_data_age <- tibble::enframe(dummy_data_age, "unit", "age") %>% select("age")
dummy_data_income <- tibble::enframe(dummy_data_income, "unit", "income") %>% select("income")
dummy_data_gender <- tibble::enframe(dummy_data_gender, "unit", "gender") %>% select("gender")
dummy_data_region <- tibble::enframe(dummy_data_region, "unit", "region") %>% select("region")
dummy_data_df <- bind_cols(dummy_data_age, dummy_data_income, dummy_data_gender, dummy_data_region)

The error message I get

> dummy_data_age <- tibble::enframe(dummy_data_age, "unit", "age") %>% select("age")
Error in select(., "age") : unused argument ("age")

Is there a way I can rewrite this code to get the same format it would have been. I'm not sure what it would have been though, as i'd just run it without checking it. FYR it was taken from this site https://rpubs.com/anthonybmasters/survey-weights-in-r Any help greatly appreciated. Thanks

H.Cheung
  • 855
  • 5
  • 12
  • I don't get any errors when running the code. It works fine for me. Tested with R4.0.5 and `tibble_3.1.2` and `dplyr_1.0.7`. Though I would say this is a very weird way to build a tibble that I probably wouldn't recommend. Are you sure you re-ran all lines starting at the top? This code reuses variable names in a potentially dangerous way if you run lines out of order. – MrFlick Jun 30 '21 at 01:11
  • I was running the latest 4.1.0. I've changed back to my previous 4.0.3 and now it works. – H.Cheung Jun 30 '21 at 02:11

2 Answers2

2

It appears that you have not loaded the library for select. After adding library(tidyverse) before your code, it worked on my machine.

library(tidyverse)
dummy_data_age <- sample(c(18:90), 100, replace = TRUE)
dummy_data_income <- sample(c(0:100000), 100, replace = TRUE)
dummy_data_gender <- sample(c("Male", "Female"), 100, replace = TRUE)
dummy_data_gender <- as.integer(factor(dummy_data_gender))
dummy_data_region <- sample(c("North", "East", "South", "West"), 100, replace = TRUE)
dummy_data_region <- as.integer(factor(dummy_data_region))
dummy_data_age <- tibble::enframe(dummy_data_age, "unit", "age") %>% select("age")
dummy_data_income <- tibble::enframe(dummy_data_income, "unit", "income") %>% select("income")
dummy_data_gender <- tibble::enframe(dummy_data_gender, "unit", "gender") %>% select("gender")
dummy_data_region <- tibble::enframe(dummy_data_region, "unit", "region") %>% select("region")
dummy_data_df <- bind_cols(dummy_data_age, dummy_data_income, dummy_data_gender, dummy_data_region)
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
0

This is because there is another library that you have loaded which masks the select function. Most probably that is MASS.

You can reproduce the error with

mtcars %>% MASS::select(cyl)

Error in MASS::select(., cyl) : unused argument (cyl)

Restarting R unloads the MASS library and it works as expected. I don't think this is R version issue. In your current setting if you used dplyr::select it should have worked as well since you are specifically mentioning the package to use select function from.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213