-1

I have a dataset called staffpai that i added variable labels to using expss's apply_labels() function. When i subset from that dataset, all labels disappear and I kind of would like to keep them. How do i either keep them, or what is another variable label function that i should use instead?

cc_limited <- subset(staffpai, close_code_cat=="Limited Service", select=(-c(number, open_date, close_date, birth_date)))

Thanks

EDIT: Ok, i did this, and for some reason the labels came forward. ?? Originally, i applied the labels days ago, so when i subset, i didn't reapply library(expss) when i ran subset(), because why would i? I was done with labels. But apparently that is required???? Because below is an example of exactly what i did, but with mtcars and the labels come with it just fine. The only difference being, i hadn't shut down R in between. So i shut down R, and re-ran library(expss), and then re-ran the subset, without reapplying labels, and they came through just fine.

library(expss)

mtcars2 = apply_labels(mtcars, mpg = "miles per gallon", cyl = "cylinders", hp = "horsepower")

mtcars3 <- subset(mtcars2, cyl=="6", select=(-c(disp, drat, wt, qsec, vs, am, gear, carb)))

getoffmylap
  • 95
  • 10
  • 3
    Without knowing the composition of `staffpai`, it's difficult to know for sure what is going on. Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Nov 04 '19 at 15:17
  • Ok, i think i figured it out. Weird, but i figured it out. Just another fun fact of R! – getoffmylap Nov 04 '19 at 15:39
  • 1
    If you want to answer your own question, you should post it as an answer below. Do not edit your question to include an "answer." That way the question can be marked as answered. – MrFlick Nov 04 '19 at 15:58
  • @getoffmylap It seems you load another package with labels support after `expss`. And this package overrided `expss` subsetting methods for class `labelled`. – Gregory Demin Nov 04 '19 at 18:51

1 Answers1

3

Ok, i did this, and for some reason the labels came forward. ?? Originally, i applied the labels days ago, so when i subset, i didn't reapply library(expss) when i ran subset(), because why would i? I was done with labels. But apparently that is required???? Because below is an example of exactly what i did, but with mtcars and the labels come with it just fine. The only difference being, i hadn't shut down R in between. So i shut down R, and re-ran library(expss), and then re-ran the subset, without reapplying labels, and they came through just fine.

library(expss)

mtcars2 = apply_labels(mtcars,
                      mpg          = "miles per gallon",
                      cyl      = "cylinders",
                      hp   = "horsepower")

mtcars3 <- subset(mtcars2, cyl=="6", 
                     select=(-c(disp, drat, wt, qsec, 
                                vs, am, gear, carb)))
getoffmylap
  • 95
  • 10