0

I use Windows and R 3.5.0

I am a beginner and have a problem with a basic code. I have defined a function which takes 3 arguments - findmean("path", "variable", id). The data frame has several columns and I have to find the mean of one of the columns. When I find the mean of the "AGE" column using

mean(finaldata$AGE,na.rm=TRUE)

and run

findmean("path", id = 1: 5)

i get the correct mean, but when i change it to

mean(finaldata$variable, na.rm = TRUE)

and run

findmean("path", "AGE", id = 1:5)

I am getting an error: argument is not numeric or logical: returning NA.

Please tell me where i am going wrong.

  • 1
    Use `[[` instead of `$` i.e. `finaldata[[variable]]` Not clear what `path` and `id` signifies in the `mean` Are you subsetting based on 'id's? – akrun Jun 27 '18 at 20:43
  • 1
    What is the purpose of your `findmean` function that you cannot do with just the `mean` function? Can you post the function? – Melissa Key Jun 27 '18 at 20:45
  • I think your title should be *"How to pass a string as argument to a function?"* or better still *"How to select a dataframe column with a string variable name?"* – smci Jun 27 '18 at 22:04
  • 1
    @akrun It worked. Thank you. – Anupam Acharya Jun 28 '18 at 05:07

0 Answers0