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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:01:59+00:00 2026-06-18T04:01:59+00:00

I am trying to apply different functions to different rows based on the value

  • 0

I am trying to apply different functions to different rows based on the value of a string in an adjacent column. My dataframe looks like this:

type    size
A       1
B       3
A       4
C       2
C       5
A       4
B       32
C       3

and I want to apply different functions to types A, B, and C, to give a third column column “size2.” For example, let’s say the following functions apply to A, B, and C:

for A: size2 = 3*size
for B: size2 = size
for C: size2 = 2*size 

I’m able to do this for each type separately using this code

df$size2 <- ifelse(df$type == "A", 3*df$size, NA)
df$size2 <- ifelse(df$type == "B", 1*df$size, NA)
df$size2 <- ifelse(df$type == "C", 2*df$size, NA)

However, I can’t seem to do it for all of the types without erasing all of the other values. I tried to use this code to limit the application of the function to only those values that were NA (i.e., keep existing values and only fill in NA values), but it didn’t work using this code:

df$size2 <- ifelse(is.na(df$size2), ifelse(df$type == "C", 2*df$size, NA), NA)

Does anyone have any ideas? Is it possible to use some kind of AND statement with “is.na(df$size2)” and “ifelse(df$type == “C””?

Many thanks!

  • 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-18T04:02:01+00:00Added an answer on June 18, 2026 at 4:02 am

    if you want you can nest the ifelses:

    df$size2 <- ifelse(df$type == "A", 3*df$size,
                           ifelse(df$type == "B", 1*df$size,
                               ifelse(df$type == "C", 2*df$size, NA)))
    
    
    #    > df
    #  type size size2
    #1    A    1     3
    #2    B    3     3
    #3    A    4    12
    #4    C    2     4
    #5    C    5    10
    #6    A    4    12
    #7    B   32    32
    #8    C    3     6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to apply different background color to even and odd items in a
im trying to apply this JSON code to my Google Map: [ { stylers:
I am trying to apply smoothing filter to image . But I get this
I'm really lost on this case. I am trying to apply a custom MVC
I'm reading and trying to apply this article about File Uploads , but there
I am trying to apply Drag and Drop function on different images that i
I'm trying to apply border-radius effect on a particular div. The code I used
i'm trying to apply the MVVM pattern in my application with MVVM Light. I've
I am trying to apply global application styles to certain control types, however adding
I'm trying to apply contrast and brightness to a bitmap in memory and I'm

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.