How do I write a function in r to do cacluations on a record?

后端 未结 2 1164
失恋的感觉
失恋的感觉 2021-01-21 14:42

In C# I am used to the concept of a data set and a current record. It would be easy for me to write a complicated calc-price function with conditions on the current record.

2条回答
  •  走了就别回头了
    2021-01-21 15:30

    For my own notes on Gregor's answer

    IsPrettyIf <-function(row){
     ret ="N"  
     if(row$Petal.Width > 0.3) { ret="Y"}
     return(ret)
    }
    
     
    df <- iris
    df$PrettyLoop ="" # add a column and initialize all the cells to be empty
    for(i in 1:5) {
      df$PrettyLoop[i] = IsPrettyIf(df[i,])
      cat("Row",i, "is Pretty?",df$PrettyLoop[i],"\n")
    }
    

    The bit that trips me up is that row$PrettyLoop is like a cell and df$PrettyLoop is like a column, thinking with the spreadsheet analogy.

提交回复
热议问题