Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:47:44+00:00 2026-06-08T16:47:44+00:00

I’m writing my first function ever (including any other programming language) and I’m a

  • 0

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))

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'}

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?

Lastly – if I wanted to add the output of the function to the dataframe, is apply the correct way to do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:47:48+00:00Added an answer on June 8, 2026 at 4:47 pm

    An answer in two parts:

    1. A tip
    2. A fix

    The tip:

    My first tip is to use a code editor that does bracket matching. For example, in Notepad++ you get this:

    PS. I’m not recommending Notepad++ – use Rstudio instead – I’m simply using Notepad++ because of the garish (and thus easy to spot) colours

    enter image description here

    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:

    enter image description here

    OK, now there is no matching brace (no highlighted red), so you need to add the missing brace at the end of your function:

    enter image description here


    The fix:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.