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
  • 32,751
  • 5
  • 50
  • 63
R.Merritt
  • 477
  • 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
  • 874,273
  • 37
  • 540
  • 662