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

  • Home
  • SEARCH
  • 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 1049173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:34:27+00:00 2026-05-16T16:34:27+00:00

I would like to apply some function on each row of a dataframe in

  • 0

I would like to apply some function on each row of a dataframe in R.

The function can return a single-row dataframe or nothing (I guess ‘return ()’ return nothing?).

I would like to apply this function on each of the rows of a given dataframe, and get the resulting dataframe (which is possibly shorter, i.e. has less rows, than the original one).

For example, if the original dataframe is something like:

id size name
1  100  dave
2  200  sarah
3  50   ben

And the function I’m using gets a row n the dataframe (i.e. a single-row dataframe), returns it as-is if the name rhymes with “brave”, otherwise returns null, then the result should be:

id size name
1  100  dave

This example actually refers to filtering a dataframe, and I would love to get both an answer specific to this kind of task but also to a more general case when even the result of the helper function (the one that operates on a single row) may be an arbitrary data frame with a single row. Please note than even in the case of filtering, I would like to use some sophisticated logic (not something simple like $size>100, but a more complex condition that is checked by a function, let’s say boo(single_row_df).

P.s.
What I have done so far in these cases is to use apply(df, MARGIN=1) then do.call(rbind ...) but I think it give me some trouble when my dataframe only has a single row (I get Error in do.call(rbind, filterd) : second argument must be a list)

UPDATE

Following Stephen reply I did the following:

ranges.filter <- function(ranges,boo) {
    subset(x=ranges,subset=!any(boo[start:end]))
}

I then call ranges.filter with some ranges dataframe that looks like this:

start end
100   200
250   400
698   1520
1988  2147
...

and some boolean vector

(TRUE,FALSE,TRUE,TRUE,TRUE,...)

I want to filter out any ranges that contain a TRUE value from the boolean vector. For example, the first range 100 .. 200 will be left in the data frame iff the boolean vector is FALSE in positions 100 .. 200.

This seems to do the work, but I get a warning saying numerical expression has 53 elements: only the first used.

  • 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-16T16:34:27+00:00Added an answer on May 16, 2026 at 4:34 pm

    You may have to use lapply instead of apply to force the result to be a list.

    > rhymesWithBrave <- function(x) substring(x,nchar(x)-2) =="ave"
    > do.call(rbind,lapply(1:nrow(dfr),function(i,dfr)
    +                      if(rhymesWithBrave(dfr[i,"name"])) dfr[i,] else NULL,
    +                      dfr))
      id size name
    1  1  100 dave
    

    But in this case, subset would be more appropriate:

    > subset(dfr,rhymesWithBrave(name))
      id size name
    1  1  100 dave
    

    If you want to perform additional transformations before returning the result, you can go back to the lapply approach above:

    > add100tosize <- function(x) within(x,size <- size+100)
    > do.call(rbind,lapply(1:nrow(dfr),function(i,dfr)
    +                      if(rhymesWithBrave(dfr[i,"name"])) add100tosize(dfr[i,])
    +                      else NULL,dfr))
      id size name
    1  1  200 dave
    

    Or, in this simple case, apply the function to the output of subset.

    > add100tosize(subset(dfr,rhymesWithBrave(name)))
      id size name
    1  1  200 dave
    

    UPDATE:

    To select rows that do not fall between start and end, you might construct a different function (note: when summing result of boolean/logical vectors, TRUE values are converted to 1s and FALSE values are converted to 0s)

    test <- function(x)
      rowSums(mapply(function(start,end,x) x >= start & x <= end,
                     start=c(100,250,698,1988),
                     end=c(200,400,1520,2147))) == 0
    
    subset(dfr,test(size))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.