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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:13:41+00:00 2026-05-31T13:13:41+00:00

I am attempting to create a data set from an original dataframe (in either

  • 0

I am attempting to create a data set from an original dataframe (in either R or Excel VBA code), and I am trying to generate another column. Here is a high-level situation:

dfr <- data.frame(
    grp   = rep(c("X", "Y"), each = 4),
    id    = c("A", "B", "C", "D", "E", "A", "B", "F"),
    value = c(3, 7, 2, 4, 8, 9, 11, 2)
) 

Of the second column, B is the “leader” of both group “X” and “Y” as it has the largest numbers. Thus, I need to pair all the observations other observations in the relevant groups (X, Y) to these leaders. For example, a sample output is below of what I need:

X  B A 3
X  B C 2
X  B D 4
Y  B E 8
Y  B A 9
Y  B F 2

The number on the furthest column is the respective number of the datapoint previously found.

So, I need help partitioning the data between X and Y (for countless string groups that exist) and afterwards, sorting it in the way I need and having that column produced, either in R-code or VBA for excel (the data is in CSV format)

**Disclaimer: If it isn’t obvious, my use of R is very limited – I used it for 4 months in an Applied Econometrics course, and now am finding myself in need of it again (9months later) so please excuse me if I seem like a novice…I can run regressions just fine though 🙂

*UPDATE
Following Henry’s code, this is where I am now.

data <- read.csv(file = "sort.csv", h=T)
attach(data)
sorted <- data[order(data$membernumber, -data$dailycirc),]
top <- function(df){ return(df[1,])}
moded <- unsplit(lapply(split(sorted, sorted$membernumber), top), unique(sorted$membernumber))[1:2]
names(moded) <- c("membernumber", "cnty")
merged <- merge(moded, data, by="membernumber")
merged[merged$cnty != merged$cnty, ]
summary(merged)

This has actually now given me some output. But I’m not seeing the sort, just a summary statistic of things like the mean/max. How do I actually export this into a CSV or spreadsheet so I can look at it like a table?

Thank you SO much for your help.

  • 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-31T13:13:42+00:00Added an answer on May 31, 2026 at 1:13 pm

    Since only R people seem to have waken up I felt obliged to post an example in VBA. I assume that you have opened your CSV in Excel with information in the first 3 columns, no title line (data starts on row 1). You copy that
    data to a new spreadsheet, in a tab named “Sheet1”. You insert the code below in a new module and run it, and it will output the result in columns 5 to 8 of the same sheet. For 10,000 lines input, it runs in less than 0.1 sec on my machine.

    Option Explicit
    
    Public Sub doIt()
    
        Dim data As Variant
        Dim result As Variant
        Dim i As Long
        Dim j As Long
        Dim dict1 As Variant
        Dim dict2 As Variant
    
        Set dict1 = CreateObject("Scripting.Dictionary")
        Set dict2 = CreateObject("Scripting.Dictionary")
        data = Sheets("Sheet1").UsedRange
    
        For i = LBound(data, 1) To UBound(data, 1)
            If dict1.exists(data(i, 1)) Then
                If dict2(data(i, 1)) < data(i, 3) Then
                    dict1(data(i, 1)) = data(i, 2)
                    dict2(data(i, 1)) = data(i, 3)
                End If
            Else
                dict1(data(i, 1)) = data(i, 2)
                dict2(data(i, 1)) = data(i, 3)
            End If
        Next i
    
        ReDim result(LBound(data, 1) To UBound(data, 1) - dict1.Count, 1 To 4) As Variant
    
        j = 1
        For i = LBound(data, 1) To UBound(data, 1)
            If data(i, 2) <> dict1(data(i, 1)) Then
                result(j, 1) = data(i, 1)
                result(j, 2) = dict1(data(i, 1))
                result(j, 3) = data(i, 2)
                result(j, 4) = data(i, 3)
                j = j + 1
            End If
        Next i
    
        With Sheets("Sheet1")
            .Cells(1, 5).Resize(UBound(result, 1), UBound(result, 2)) = result
        End With
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create a small application to collect data received from an
I am attempting to create a template that will utilize data from an Access
I'm attempting to use the DataSet designer to create a datatable from a query.
I'm attempting to create Data Access Layer for my web application. Currently, all datatables
I'm attempting to create a custom calendar control that inherits from ASP.Net's built in
I am attempting to write some Java code that takes an image from a
I'm attempting to generate a grid of divs five elements wide from the objects
I've been going bananas trying to get some data from Javascript on one page
I have a data set that i am attempting to select the first record
I am attempting to create the the business and data layers for my big

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.