0

This may be a trivial question, but I am struggling to address it. I am trying to index a named variable in a function. Take the following example. I would like to assign labels to my variables. I know I can do this with the following lines of code

iris<–iris
attr(iris$Sepal.Width, "label") <- "Label 1"
library(Hmisc)
contents(iris)
              Labels Levels Storage
Sepal.Length                 double
Sepal.Width  Label 1         double # my variable is labelled
Petal.Length                 double
Petal.Width                  double
Species                   3 integer

However, if I try to create a function that does the same operation, I incur into an error because I am unable to index named variables in user-written functions. For instance, if I create a function labvar thus defined i incur into an error.

labvar <- function(var,lab){
attr(iris[[var]], "label") <- lab
}
labvar("Sepal.Length", "label 2")
             Labels Levels Storage
Sepal.Length                 double # variable remains not labelled
Sepal.Width  Label 1         double
Petal.Length                 double
Petal.Width                  double
Species                   3 integer
 

I have also tied multiple alternatives to index the variable with iris[[var]],iris[var],iris{{var}} but none of these approach work. Can anyone help me and explain how I can index a named variable as I would do with $ outside the function?

thanks a lot in advance for your help

Alex
  • 1,207
  • 9
  • 25
  • What exactly are you trying to do here? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You [can't use $ with a variable](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value) but you can use `[[]]` but it doesn't look like that's really the problem here. What exactly "doesn't work" for you? Do you get an error? – MrFlick Jan 22 '21 at 17:23
  • I have edited the question, i hope it is clearer. unfortunately `[[]]` does not work – Alex Jan 22 '21 at 17:31
  • 1
    You need to be more clear on what "Doesn't work" means. What does or does not happen when you run the code. What is the expected value after you run the function? It seems like maybe you are assigning values the wrong way. Did you mean `attr(dt[[var]], "label") <- lab`? And them make sure to call the function with quotes `labvar("Sepal.Length", "label 1")`. But you are using `dt` in the function and that's not defined anywhere which is odd. – MrFlick Jan 22 '21 at 17:36

1 Answers1

2

There are a lot of syntax/function issues going on here.

  • [[ works with character vectors, so a function with body attr(iris[[var]], "label") would expect var = "Speal.Length" as an argument, not var = Sepal.Length
  • Your use of -> inside the function is wrong - you want to assign the label to the attribute, not the attribute to the label
  • And your function should probably take the whole data frame as input,
  • and return the modified data frame - as-is,
  • and similarly you need to assign the result

Addressing each of these:

lab_var = function(data, var, lab) {
  attr(data[[var]], "label") = lab
  return(data)
}

labeled_iris = lab_var(iris, "Sepal.Length", "my_label")
attr(labeled_iris$Sepal.Length, "label")
# [1] "my_label"
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Dear @GregorThomas, thank you very much for your explanation, but I fear the solution still does not work. I have tried `lab_var` function on the data, but unfortunately the variable remains unlabelled. I have also edited the question, It may help clarify a few points (thanks for spotting typos) – Alex Jan 22 '21 at 17:54
  • Also, it would be ideal if it would be possible to index the variable without using the `""`. But I am not sure how it would work – Alex Jan 22 '21 at 17:55
  • I don't know what you mean by "does not work" - I show a reproducible example where it does work, and I demonstrate getting `"my_label"` out. You'll need to be much more specific. – Gregor Thomas Jan 22 '21 at 18:03
  • Thanks a lot it works well and it is very clear! just a couple of questions 1) is there a specific reason why you used `=` rather than `<-` to assign the function? also, is there a way to index without having to use the `""`? thanks a lot in advance – Alex Jan 22 '21 at 18:05
  • 1
    I just prefer `=` to `<-` - it makes me an oddball in the R community. Switch to `<-` if you prefer. As for skipping the quotes, you can use `substitute()` - see some of the examples [in this FAQ](https://stackoverflow.com/a/36015931/903061) – Gregor Thomas Jan 22 '21 at 18:16