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 8586279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:16:24+00:00 2026-06-11T22:16:24+00:00

I’m looking for a way to perform multiple variable assignments based on a single

  • 0

I’m looking for a way to perform multiple variable assignments based on a single conditional statement. The ifelse function performs what I want for a single variable at a time, but I’d like to be able to perform a block of statements based on a single condition.

Here is a bit of simplified example code:

within(mydata, {
  if (gender == "f") {
    test1 <- 1
    test2 <- 2
  } else {
    test1 <- 0
    test2 <- 0
  }
  test3 <- gender
  test4 <- ifelse(gender == "f", 1, 0)
  test5 <- ifelse(gender == "f", 2, 0)  
})

Which gives the following output:

  workshop gender q1 q2 q3 q4 test5 test4 test3 test2 test1
1        1      f  1  1  5  1     2     1     f     2     1
2        2      f  2  1  4  1     2     1     f     2     1
3        1      f  2  2  4  3     2     1     f     2     1
4        2      f  3  1 NA  3     2     1     f     2     1
5        1      m  4  5  2  4     0     0     m     2     1
6        2      m  5  4  5  5     0     0     m     2     1
7        1      m  5  3  4  4     0     0     m     2     1
8        2      m  4  5  5  5     0     0     m     2     1
Warning message:
In if (gender == "f") { :
  the condition has length > 1 and only the first element will be used

When I run this code, test4 and test5 are correctly assigned, but test1 and test2 are incorrectly assigned, because the if statement returns the value for the first row only. Is there a way to do what I’m trying to do with test1 and test2 – run multiple statements for each row of a data frame based on a single condition?

I know I can accomplish the same result with ifelse, but I’d like to be able to group the statements together, for clarity when reading my code.

For example, I’d like to be able to group the savings calculations that I do by measure, as follows:

a.lighting.all.3 <- within(a.lighting.all.3, {
  if (measure.subcategory %in% c('HID to Linear Fluorescent Retrofit', 
                                 'Hardwired CFL', 'Induction Lighting', 
                                 'Screw-In CFL', 'Specialty Screw-In CFL',
                                'T12 to Premium T8/T5', 'T12 to Standard T8/T5',
                                 'T8 to Premium T8', 'T12/T8 Delamping')) {
    kw.nc.v <- (base.watts - ee.watts) / 1000 * (1 + dif) * df * quantity
    kwh.v <- (base.watts - ee.watts) / 1000 * (1 + eif) * op.hrs * quantity    
  } else if (measure.subcategory == 'Traffic Signals') {
    kw.nc.v <- (base.watts - ee.watts) / 1000 * quantity
    kwh.v <- (base.watts - ee.watts) / 1000 * op.hrs * quantity    
  } else if (measure.subcategory == 'Exit Sign Retrofit') {

  } else if (measure.subcategory %in% c('LED Channel Lights',
                                        'Cold Cathode FL')) {
  } else if (measure.subcategory %in% c('Daylighting Controls', 
                                        'Occupancy Sensors')) {

  } else if (measure.subcategory == 'Lighting Power Density') {

  } else if (measure.subcategory == 'LED Lighting') {

  }
}) 

Or assign sets of parameters by measure, such as:

a.lighting.all.3 <- within(a.lighting.all.3, {
  switch(as.character(measure.subcategory),
     "T8 to Premium T8" = {
       op.hrs <- 4481
       cf <- 0.93
     },
     "Cold Cathode FL" = {
       op.hrs <- 6400
       cf <- 1
     },
     "Exit Sign Retrofit" = {
       op.hrs <- 8760
       cf <- 1
     },
     "LED Channel Lights" = {
       op.hrs <- 5110
       cf <- 0.134
     },
     "Traffic Signals" = {
       op.hrs <- ifelse(grepl("Green", measure), 3679, 4818)
       df <- ifelse(grepl("Green", measure), 0.42, 0.55)
       cf <- 1
     },
     "Daylighting Controls" = {
       dsf <- esf <-  0.54  # daylight savings fraction
     },
     "Occupancy Sensors" = {
       dsf <- 0.16  # demand savings fraction
       esf <- 0.39  # energy savings fraction
     },
     "LED Lighting" = {
       if (measure %in% c("Pedestrian NO countdown", 
                          "Pedestrian W/ countdown")) {
         cf <- 1
         op.hrs <- ifelse(measure == "Pedestrian W/ countdown", 6483, 5432)
         op.hrs.base <- 5432
         df <- ifelse(measure == "Pedestrian W/ countdown", 0.74, 0.62)
         df.base <- 0.62
       } else if (measure %in% c("Refrigerated Case LED Lamps NO motion Sensors",
                                 "Refrigerated Case LED Lamps W/ motion Sensors")) {
         cf <- 1
         dif <- 0.25
         eif <- 0.25
         op.hrs.base <- 8634
         op.hrs <- ifelse(measure == "Refrigerated Case LED Lamps W/ motion Sensors",
                          6043, 8634)
       }
     }
  )
})

Any ideas?

  • 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-11T22:16:25+00:00Added an answer on June 11, 2026 at 10:16 pm
    d <- data.frame(workshop=rep(1:2,4),
                    gender=rep(c("f","m"),each=4))
    

    I don’t know if this answer makes you happy or not, but: if you use plyr to operate on blocks of code you can do what you want with an if statement.

    library(plyr)
    ddply(d,"gender",
          function(x) {
              within(x, {
                  test3 <- gender
                  ## test FIRST value only, since by construction they
                  ## are all the same within a piece
                  if (gender[1]=="f") {
                      test1 <- 1
                      test2 <- 2
                      test4 <- 1
                      test5 <- 1
                  } else {
                      test1 <- test2 <- test4 <- test5 <- 0
                  }})
          })
    

    Note that this will rearrange your data into blocks by gender (in this example it doesn’t change anything), which might be undesirable …

    I didn’t include the additional variables in my example, but they will get carried along correctly.

    • 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
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.