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