1

I am trying to access a column in a data frame using a variable , this variable wil be populated in a loop

atr<-"yield_kgha"

so what i want is the second line below where $atr to act like it was $yield_kgha

I tried $get(atr) with no luck ... how do I get atr to be taken literally

meanis=MEAN = mean(zones[[zonename]]$yield_kgha , na.rm = TRUE) #get the mean yield_kgha in the zone
meanis=MEAN = mean(zones[[zonename]]$atr , na.rm = TRUE) #get the mean yield_kgha in the zone
neilfws
  • 29,020
  • 5
  • 49
  • 59
R.Merritt
  • 425
  • 5
  • 17

1 Answers1

0

If we want to use an object, then use [[ instead of $ similar to the 'zonename'

mean(zones[[zonename]][[atr]] , na.rm = TRUE) #
akrun
  • 789,025
  • 32
  • 460
  • 575