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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:05:07+00:00 2026-06-13T07:05:07+00:00

I have been scratching my head over this. I have two data frames: df

  • 0

I have been scratching my head over this. I have two data frames: df

df <- data.frame(group = 1:3,
                 age = seq(30, 50, length.out = 3),
                 income = seq(100, 500, length.out = 3),
                 assets = seq(500, 800, length.out = 3))

and weights

weights <- data.frame(age = 5, income = 10)

I would like to multiply these two data frames only for the same column names. I tried something like this:

colwise(function(x) {x * weights[names(x)]})(df)

but that obviously didn’t work as colwise does not keep the column name inside the function. I looked at various mapply solutions (example), but I am unable to come up with an answer.

The resulting data.frame should look like this:

structure(list(group = 1:3, age = c(150, 200, 250), income = c(1000, 
3000, 5000), assets = c(500, 650, 800)), .Names = c("group", 
"age", "income", "assets"), row.names = c(NA, -3L), class = "data.frame")

  group age income assets
1     1 150   1000    500
2     2 200   3000    650
3     3 250   5000    800
  • 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-13T07:05:08+00:00Added an answer on June 13, 2026 at 7:05 am

    sweep() is your friend here, for this particular example. It relies upon the names in df and weights being in the right order, but that can be arranged.

    > nams <- names(weights)
    > df[, nams] <- sweep(df[, nams], 2, unlist(weights), "*")
    > df
      group age income assets
    1     1 150   1000    500
    2     2 200   3000    650
    3     3 250   5000    800
    

    If the variable names in weights and df are not in the same order, you can make them so:

    > df2 <- data.frame(group = 1:3,
    +                   age = seq(30, 50, length.out = 3),
    +                   income = seq(100, 500, length.out = 3),
    +                   assets = seq(500, 800, length.out = 3))
    > nams <- c("age", "income") ## order in df2
    > weights2 <- weights[, rev(nams)]
    > weights2  ## wrong order compared to df2
      income age
    1     10   5
    > df2[, nams] <- sweep(df2[, nams], 2, unlist(weights2[, nams]), "*")
    > df2
      group age income assets
    1     1 150   1000    500
    2     2 200   3000    650
    3     3 250   5000    800
    

    In other words we reorder all objects so that age and income are in the right order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been scratching my head over this for an hour... I have a list
I have been scratching my head for hours trying to figure out why this
I've been scratching my head for hours trying to figure this out. I have
I have been scratching my head over this for days and I still cannot
I'm really confused and have been scratching my head over this for a few
Okay guys, I have been scratching my head like mad over this issue and
Been scratching my head for a day over this one. I have a few
I have been scratching my head for a while on this one. I dont
I have been scratching my head all morning behind this but still haven't been
I've been scratching my head for a while trying to figure this out. I

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.