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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:17:54+00:00 2026-06-18T18:17:54+00:00

Question: How to apply functions to a subset of the data in a vectorized

  • 0

Question:

How to apply functions to a subset of the data in a vectorized manner.

Example:

For the data frame below:

x=c(1,2,1,2,1,2)
y=c(3,4,5,4,3,2)
df=data.frame(x,y)

I would like to apply a function (i.e. min()) to all y values for each of the x value, and collect it in a vector.

Basically, I would like to have a vectorized version of this:

nb = max(x);
V = rep(0.0, nb)
for(i in 1:nb){
    v = df [ x == i,  ]$y;
    V[i] <- min(v);
}

# basically here:
# V[1] = min( df$y for x=1)
# V[2] = min( df$y for x=2)
  • 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-18T18:17:55+00:00Added an answer on June 18, 2026 at 6:17 pm

    The function tapply is designed for such problems:

    with(df,tapply(y,x,FUN=min))
    #1 2 
    #3 2
    

    If you want to add the results to your data frame, you can use the function ave:

    df$group.min <- with(df,ave(y,x,FUN=min))
    #   x y group.min
    # 1 1 3         3
    # 2 2 4         2
    # 3 1 5         3
    # 4 2 4         2
    # 5 1 3         3
    # 6 2 2         2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar to this R question , I'd like to apply a function to each
I have a small question about apply functions. For example I have: l <-
I recentely asked a question about using an apply function over two lists. Each
I have a question on how to apply R functions to multidimensional arrays. For
I would like to ask this question, because I'm not sure if I got
My question is I wanna apply strategy pattern to save log from Image above
this is another question that I want to know that how to apply any
I came across this question on transcender: What should you apply to a field
This question is related to my ASP.NET MVC 2 development, but it could apply
I have a data.frame of cells, values and coordinates. It resides in the global

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.