0

I have written a function that I want to get a label and returns the oldest group that contains the label.My data is as follows and the age column represents the age of the group.

  >df
  x y label age num_group
  a 1     T  23         1
  b 5     F  12         2
  c 3     A  30         3
  d 2     T   1         4

And my function is as follows

 groups<-data.frame
 oldgroup<-function(lbltst){
 for(k in 1:nrow(df)){
 if(df[k,3]==lbltst){
 groups[k,]<-df[k,]}}
 minage<-min(groups$age)
 old<-groups[which(groups$age==minage)[1],]
 return(old)}

But after calling the function, the following error message will be given. Can anyone help me fix this function?

lbltst<-"T"
d<-oldgroup(lbltst)
Error: object of type 'closure' is not subsettable

Thank you very much for your help

maria
  • 45
  • 6
  • From the top answer to the question this is a duplicate of: "In general this error message means that you have tried to use indexing on a function." ([source](https://stackoverflow.com/a/11308796/8386140)). In particular, you assign the function `data.frame()` to the variable `groups` via `groups<-data.frame` then try to subset it several times in your function, for example via `groups[k,]<-df[k,]` – duckmayr Nov 25 '17 at 11:12
  • like @duckmayr said, this seems like a typo, in `groups<-data.frame` you are forgetting the parenthesis. I'm voting to close as off-topic. – Rui Barradas Nov 25 '17 at 11:56

0 Answers0