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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:47:09+00:00 2026-06-12T14:47:09+00:00

Looking through the ave function, I found a remarkable line: split(x, g) <- lapply(split(x,

  • 0

Looking through the ave function, I found a remarkable line:

split(x, g) <- lapply(split(x, g), FUN) # From ave

Interestingly, this line changes the value of x, which I found unexpected. I expected that split(x,g) would result in a list, which could be assigned to, but discarded afterward. My question is, why does the value of x change?

Another example may explain better:

a <- data.frame(id=c(1,1,2,2), value=c(4,5,7,6))
#   id value
# 1  1     4
# 2  1     5
# 3  2     7
# 4  2     6

split(a,a$id) # Split a row-wise by id into a list of size 2
# $`1`
#   id value
# 1  1     4
# 2  1     5
# $`2`
#   id value
# 3  2     7
# 4  2     6

# Find the row with highest value for each id
lapply(split(a,a$id),function(x) x[which.max(x$value),])
# $`1`
#   id value
# 2  1     5
# $`2`
#   id value
# 3  2     7

# Assigning to the split changes the data.frame a!
split(a,a$id)<-lapply(split(a,a$id),function(x) x[which.max(x$value),])
a
#   id value
# 1  1     5
# 2  1     5
# 3  2     7
# 4  2     7

Not only has a changed, but it changed to a value that does not look like the right hand side of the assignment! Even if assigning to split(a,a$id) somehow changes a (which I don’t understand), why does it result in a data.frame instead of a list?

Note that I understand that there are better ways to accomplish this task. My question is why does split(a,a$id)<-lapply(split(a,a$id),function(x) x[which.max(x$value),]) change a?

  • 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-12T14:47:10+00:00Added an answer on June 12, 2026 at 2:47 pm

    The help page for split says in its header: “The replacement forms replace values corresponding to such a division.” So it really should not be unexpected, although I admit it is not widely used. I do not understand how your example illustrates that the assigned values “do not look like the RHS of the assignment!”. The max values are assigned to the ‘value’ lists within categories defined by the second argument factor.

    (I do thank you for the question. I had not realized that split<- was at the core of ave. I guess it is more widely used than I realized, since I think ave is a wonderfully useful function.)

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

Sidebar

Related Questions

Looking through the dom.js source from the Closure library I found this (in goog.dom.getElementsByTagNameAndClass_
Looking through style sheets from popular & unpopular websites I have found the div
Looking through the documentation for Highstocks, I found this example : Shown here is
Was looking through the beloved W3schools and found this page and actually learned something
Looking through this C++ BigInt library and found the BigInt.cpp file. At the top
When looking through some code I took over, I came across this line: my
While looking through some code (javascript), I found this piece of code: <script>window.Bootloader &&
While looking through the transformers package, I found this monad transformer called IdentityT .
While looking through some old code I came across this gem: MyObject o =
Looking through my VisualSVNServer logs, I'm seeing several attempts from different client IP addresses

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.