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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:21:43+00:00 2026-05-25T00:21:43+00:00

Every time I think I understand about working with vectors, what appears to be

  • 0

Every time I think I understand about working with vectors, what appears to be a simple problem turns my head inside out. Lot’s of reading and trying different examples hasn’t helped on this occasion. Please spoon feed me here…

I want to apply two custom functions to each row of a dataframe and add the results as a two new columns. Here is my sample code:

# Required packages:
library(plyr)

FindMFE <- function(x) {
    MFE <- max(x, na.rm = TRUE) 
    MFE <- ifelse(is.infinite(MFE ) | (MFE  < 0), 0, MFE)
    return(MFE)
}

FindMAE <- function(x) {
    MAE <- min(x, na.rm = TRUE) 
    MAE <- ifelse(is.infinite(MAE) | (MAE> 0), 0, MAE)
    return(MAE)
}

FindMAEandMFE <- function(x){
        # I know this next line is wrong...
    z <- apply(x, 1, FindMFE, FindMFE)
        return(z)
}

df1 <- data.frame(Bar1=c(1,2,3,-3,-2,-1),Bar2=c(3,1,3,-2,-3,-1))

df1 = transform(df1, 
    FindMAEandMFE(df1)  
)

#DF1 should end up with the following data...
#Bar1   Bar2    MFE MAE
#1      3       3   0
#2      1       2   0
#3      3       3   0
#-3     -2      0   -3
#-2     -3      0   -3
#-1     -1      0   -1

It would be great to get an answer using the plyr library and a more base like approach. Both will aid in my understanding. Of course, please point out where I’m going wrong if it’s obvious. 😉

Now back to the help files for me!

Edit: I would like a multivariate solution as column names may change and expand over time. It also allows re-use of the code in future.

  • 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-05-25T00:21:43+00:00Added an answer on May 25, 2026 at 12:21 am

    I think you are thinking too complex here. What is wrong with two separate apply() calls? There is however a far better way to do what you are doing here that involves no looping/apply calls. I’ll deal with these separately, but the second solution is preferable as it is truly vectorised.

    Two apply calls version

    First two separate apply calls using all-Base R functions:

    df1 <- data.frame(Bar1=c(1,2,3,-3,-2,-1),Bar2=c(3,1,3,-2,-3,-1))
    df1 <- transform(df1, MFE = apply(df1, 1, FindMFE), MAE = apply(df1, 1, FindMAE))
    df1
    

    Which gives:

    > df1
      Bar1 Bar2 MFE MAE
    1    1    3   3   0
    2    2    1   2   0
    3    3    3   3   0
    4   -3   -2   0  -3
    5   -2   -3   0  -3
    6   -1   -1   0  -1
    

    Ok, looping over the rows of df1 twice is perhaps a little inefficient, but even for big problems you’ve spent more time already thinking about doing this cleverly in a single pass than you will save by doing that way.

    Using vectorised functions pmax() and pmin()

    So a better way of doing this is to note the pmax() and pmin() functions and realise that they can do what each the apply(df1, 1, FindFOO() calls were doing. For example:

    > (tmp <- with(df1, pmax(0, Bar1, Bar2, na.rm = TRUE)))
    [1] 3 2 3 0 0 0
    

    would be MFE from your Question. This is very simple to work with if you have two columns and they are Bar1 and Bar2 or the first 2 columns of df1, always. But it is not very general; what if you have multiple columns you want to compute this over etc? pmax(df1[, 1:2], na.rm = TRUE) won’t do what we want:

    > pmax(df1[, 1:2], na.rm = TRUE)
      Bar1 Bar2
    1    1    3
    2    2    1
    3    3    3
    4   -3   -2
    5   -2   -3
    6   -1   -1
    

    The trick to getting a general solution using pmax() and pmin() is to use do.call() to arrange the calls to those two functions for us. Updating your functions to use this idea we have:

    FindMFE2 <- function(x) {
       MFE <- do.call(pmax, c(as.list(x), 0, na.rm = TRUE))
       MFE[is.infinite(MFE)] <- 0
       MFE
    }
    
    FindMAE2 <- function(x) {
       MAE <- do.call(pmin, c(as.list(x), 0, na.rm = TRUE))
       MAE[is.infinite(MAE)] <- 0
       MAE
    }
    

    which give:

    > transform(df1, MFE = FindMFE2(df1), MAE = FindMAE2(df1))
      Bar1 Bar2 MFE MAE
    1    1    3   3   0
    2    2    1   2   0
    3    3    3   3   0
    4   -3   -2   0  -3
    5   -2   -3   0  -3
    6   -1   -1   0  -1
    

    and not an apply() in sight. If you want to do this in a single step, this is now much easier to wrap:

    FindMAEandMFE2 <- function(x){
        cbind(MFE = FindMFE2(x), MAE = FindMAE2(x))
    }
    

    which can be used as:

    > cbind(df1, FindMAEandMFE2(df1))
      Bar1 Bar2 MFE MAE
    1    1    3   3   0
    2    2    1   2   0
    3    3    3   3   0
    4   -3   -2   0  -3
    5   -2   -3   0  -3
    6   -1   -1   0  -1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I think I understand about casting and conversions, I find another strange
I think beestings change the html every time. this means html is not able
I am tired of using them, Every time I think now I know these
I can't really ever think of a time when I would use git merge
Every time I turn on my company-owned development machine, I have to kill 10+
Every time I create a new project I copy the last project's ant file
Every time I create an object that has a collection property I go back
Every time I need to work with date and/or timstamps in Java I always
Every time I have to estimate time for a project (or review someone else's
Every time a user posts something containing < or > in a page in

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.