我正在写我的第一个函数(包括任何其他编程语言),并且对 if , else 和 ifelse .我搜索了很多示例,但是我都不清楚.
I'm writing my first function ever (including any other programming language) and I'm a little confused on the proper structure for if, else and ifelse. I've searched a ton of examples, but none are that clear to me.
情况-我正在尝试按客户已经拥有客户多长时间来对客户进行分类,然后将其变成一个因素.
Situation - I'm trying to bucket clients by how long they have been clients for, then turn that into a factor.
#Sample Data clientID <- round(runif(2,min=2000, max=3000),0) MonthsSinceSignUp <- round(runif(20,min=1, max=60),0) df <- data.frame(cbind(clientID,MonthsSinceSignUp))对于给定的客户,我想确定他们是否已经不足一年,超过一年但少于2年了,等等.
For a given client, I would like to determine if they have been so for less than a year, more than year, but less than 2, etc.
这是我第一次使用函数:
This is my first crack at a function:
ClientAgeRange <- function(MonthsSinceSignUp) { if (MonthsSinceSignUp < 13) {ClientAgeRange <- '1 year'} } else { if (MonthsSinceSignUp > 13 & MonthsSinceSignUps < 25) {ClientAgeRange <- '2 years'} } else {ClientAgeRage <- '3+ years'}我一直遇到的错误是:错误:}"中出现意外的'}',这表示我不见了或者有多余的右括号.但是,尽管我遇到了麻烦,但我找不到它.但是-我认为一般来说,我没有将正确的结构应用于该函数.我正在尝试生成一个 if,然后将此变量设置为该.如何正确构造此功能?
The error that I keep getting is: Error: unexpected '}' in "}", which would indicate I'm missing or have an extra closing bracket. However, despite my trouble shooting, I can't locate it. But - I think in general, I'm not apply the correct structure to the function. I'm trying to produce a if this, then set this variable as that. How can I structure this function properly?
最后-如果我想将函数的输出添加到 dataframe , apply 是正确的方法吗?
Lastly - if I wanted to add the output of the function to the dataframe, is apply the correct way to do so?
推荐答案答案分为两个部分:
提示:
我的第一个技巧是使用进行括号匹配的代码编辑器.例如,在 Notepad ++ 中,您将获得以下信息:
The tip:
My first tip is to use a code editor that does bracket matching. For example, in Notepad++ you get this:
PS.我不建议使用 Notepad ++ -而是使用Rstudio-我只是使用 Notepad ++ ,因为花哨的颜色(易于识别)
PS. I'm not recommending Notepad++ - use Rstudio instead - I'm simply using Notepad++ because of the garish (and thus easy to spot) colours
请注意,突出显示的大括号(红色)与函数中间的大括号匹配.这表明第一个 if 的末尾有多余的括号.因此,请先解决该问题:
Notice that the highlighted brace (in red) matches with a brace in the middle of your function. This reveals that there is redundant brace at the end of your first if. So, fix that first:
好的,现在没有匹配的花括号(没有突出显示的红色),因此您需要在函数末尾添加缺少的花括号:
OK, now there is no matching brace (no highlighted red), so you need to add the missing brace at the end of your function:
但是,如果使用 cut (旨在执行这种类型的分析),则可以大大简化功能:
But you can vastly simplify your function if you use cut, which is designed to do this type of analysis:
ClientAgeRange <- function(x) { cut(x, breaks=c(0, 13, 25, Inf), labels=c("1 year", "2 years", "3+ years")) }在您的代码上尝试一下:
Try it on your code:
ClientAgeRange(df$MonthsSinceSignUp) [1] 2 years 1 year 3+ years 2 years 3+ years 3+ years 2 years 2 years 3+ years 3+ years 1 year [12] 3+ years 2 years 3+ years 3+ years 3+ years 3+ years 3+ years 3+ years 3+ years Levels: 1 year 2 years 3+ years更多推荐
在R中正确构建函数
发布评论