0

I have a dataset set called people which looks like this

Year   Name Age Weight  
1993   Dan  10   100
1994   Dan  11   120
1995   Dan  12   125
1993   Bob  10   96
1994   Bob  11   120
1995   Bob  12   113

I want to split this data set and then find corrlelations between age and weight using a function taking a persons name as the argument to the function. This code works...

splitpeople<- split(people, people$Name)

cor(splitpeople$Dan$Age,splitpeople$Dan$Weight)

But when I attempt to run a function like this...

peoplefunc <- function(names)
{

splitpeople <- split(people, people$Name)

cor(splitpeople$names$Age,splitpeople$names$Weight)
}
peoplefunc(Dan)

I get an error in the cor() line saying supply both x and y or a matrix

What is going wrong in this function?

USER8000
  • 3
  • 3
  • Please provide more information so we can help. For instance, it is difficult to diagnose the problem without knowing what the error is. See the following link with tips on producing a [minimum example](http://stackoverflow.com/help/mcve) – lmo Apr 20 '16 at 22:38
  • 1
    @lmo Normally that's true but in this case it's fairly obvious what's going on. This will get closed as a duplicate soon I'm guessing. I'm too lazy to find a match but basically they're using splitpeople$names and assuming that the input into the function "names" will replace the names in the subset. They need to use double bracket indexing instead. – Dason Apr 20 '16 at 22:41
  • @Dason I agree that this is the most likely culprit, but I think it is still helpful to encourage clarity in posting a question. – lmo Apr 20 '16 at 22:46
  • I added what the error was in the question. I also tried changing the function to splitpeople <- split(people, people$Name) xy <- splitpeople[[names]] cor(xy$Age,xy$Weight) but i dont know if that is what you were sugesting? – USER8000 Apr 20 '16 at 22:50
  • I'm with Dason on this one. Clarity is good - and this issue is clear with what's provided. Coaching a new user into making nice, fully reproducible example is well worth it when their efforts are rewarded with an excellent cusom-tailored answer. When the question is a clear dupe there's not a lot of point in polishing it up. – Gregor Thomas Apr 20 '16 at 22:52
  • @USER8000 I'd especially recommend reading through joran's answer at the duplicate, it deals very directly with the issue as you're facing it. – Gregor Thomas Apr 20 '16 at 22:52
  • Thanks for this effort, @USER8000. Sorry if the polishing was a hassle. – lmo Apr 20 '16 at 22:56

0 Answers0