Q:

I build loop in a function in R. How to add the outcome to a dataframe?

I have a loop build in a function in R. How can I add the outcomes to a column in an existing data frame?
For example:
QYld<-NULL

for (i in 1:nrow(dat))

{

  ymax<-dat[i,"Ymax"]

  print(ymax)

  settings <- data.frame (waterlimited_yield=ymax,

                          recover_N = 0.5,

                          recover_P = 0.1,

                          recover_K = 0.5,

                          AvgTemp = 18)

  #Function

  tmpyld<-yield_example(olsenP=5.0, exchK=3.0 , pH=5.5, SOC=12.5, kgNha=100,kgPha=30, kgKha=30, settings)

A:

Assuming that your yield_example function returns a single value, then you could do something like this.
for (i in 1:nrow(dat)){
  ymax<-dat[i,"Ymax"]
  print(ymax)
  settings <- data.frame (waterlimited_yield=ymax,
                          recover_N = 0.5,
                          recover_P = 0.1,
                          recover_K = 0.5,
                          AvgTemp = 18)
  #Function
  dat[i,"tmpyld"] <- yield_example(olsenP=5.0, exchK=3.0 , pH=5.5, SOC=12.5, kgNha=100,kgPha=30, kgKha=30, settings)
}

