简体   繁体   English

如何使用来自 dataframe 的输入创建 function 并将其应用于所有行?

[英]how to create function with input from dataframe and apply it over all rows?

I try to write a function in R which takes several variables from a dataframe as input and gives a vector with results as output. I try to write a function in R which takes several variables from a dataframe as input and gives a vector with results as output.

Based on this post below I did write the function below.根据下面的这篇文章,我确实在下面写了 function。 How can create a function using variables in a dataframe 如何使用 dataframe 中的变量创建 function

Although I receive this warning message:虽然我收到此警告消息:

the condition has length > 1 and only the first element will be used

I have tried to solve it by the post below using sapply in the function although I do not succeed.我试图通过下面的帖子在 function 中使用 sapply 来解决它,尽管我没有成功。 https://datascience.stackexchange.com/questions/33351/what-is-the-problem-with-the-condition-has-length-1-and-only-the-first-elemen https://datascience.stackexchange.com/questions/33351/what-is-the-problem-with-the-condition-has-length-1-and-only-the-first-elemen

# a data frame with columns a, x, y and z:

myData <- data.frame(a=1:5,
                     x=(2:6),
                     y=(11:15),
                     z=3:7)


myFun3 <- function(df, col1 = "x", col2 = "y", col3 = "z"){      
   result <- 0      
   if(df[,col1] == 2){result <- result + 10
   }      
   if(df[,col2] == 11){result <- result + 100
   }      
   return(result)
}

myFun3(myData)

>    Warning messages:
>    1: In if (df[, col1] == 2) { :
>      the condition has length > 1 and only the first element will be used
>    2: In if (df[, col2] == 11) { :
>      the condition has length > 1 and only the first element will be used

Can someone explain me how I can apply the function over all rows of the dataframe?有人可以解释我如何在 dataframe 的所有行上应用 function 吗? Thanks a lot!非常感谢!

We need ifelse instead of if/else as if/else is not vectorized我们需要ifelse而不是if/else ,因为if/else没有向量化

myFun3 <- function(df, col1 = "x", col2 = "y", col3 = "z"){ 
       result <- numeric(nrow(df))
       ifelse(df[[col1]] == 2,  result + 10,
           ifelse(df[[col2]] == 11, result + 100, result))     

   }

myFun3(myData)
#[1] 10  0  0  0  0

Or the OP's code can be Vectorize d after making some changes ie remove the second if with an else if ladder或者,在进行一些更改后,OP 的代码可以Vectorize d,即使用else if阶梯删除第二个if

myFun3 <- Vectorize(function(x, y){      
   result <- 0      
   if(x == 2) {
       result <- result + 10
    } else if(y == 11){
       result <- result + 100
     } else result <- 0     
   return(result)
})
myFun3(myData$x, myData$y)
#[1] 10  0  0  0  0

Regarding the OP's doubts about when multiple conditions are TRUE, then want only the first to be executed, the ifelse (nested - if more than two) or if/else if/else ( else if ladder or if/else nested) both works because it is executed in that same order we specified the condition and it stops as soon as a TRUE condition occurred ie suppose we have multiple conditions关于 OP 对多个条件何时为 TRUE 的怀疑,然后只希望第一个被执行, ifelse (嵌套 - 如果两个以上)或if/else if/elseelse if ladder 或 if/else 嵌套)两者都有效,因为它以我们指定条件的相同顺序执行,并且一旦发生 TRUE 条件即停止,即假设我们有多个条件

 if(expr1) {
    1
 } else if(expr2) {
    2
 } else if(expr3) {
   3
 } else if(expr4) {
   4
 } else {
   5}

checks the first expression ('expr1') first, followed by second, and so on.首先检查第一个表达式 ('expr1'),然后是第二个,依此类推。 The moment it return TRUE, it exit ie it is a nested condition当它返回 TRUE 时,它退出,即它是一个嵌套条件

if(expr1) {
     1
  } else {
        if(expr2) {
          2
         } else {
             if(expr3) {
               3
              } else {
                 if(expr4) {
                  4
                  } else 5
                    }
                 }
           }

There is a cost for this ie.这是有代价的,即。 whereever we have the more values that matches the 1, only the expr1 is executed and thus saves time, but if there are more 5 values, then all those conditions are checked无论我们有更多与 1 匹配的值,只执行 expr1 从而节省时间,但如果有更多 5 个值,则检查所有这些条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM