-1

I am using cars and this has two variables: speed and dist. I am trying to create groups based on speed - so all observations with speed==4 becomes an object, all observations with speed==7 is an object, all observations with speed==10 is an object and so on.

I've tried:

> apply(subset(cars,cars$speed))
Error in match.fun(FUN) : argument "FUN" is missing, with no default
> apply(subset(by=cars$speed))
Error in match.fun(FUN) : argument "FUN" is missing, with no default
> sapply(subset(by=cars$speed))
Error in match.fun(FUN) : argument "FUN" is missing, with no default
> sapply(subset(cars,cars$speed))
Error in match.fun(FUN) : argument "FUN" is missing, with no default

and they all don't work. Please help!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Lee12345
  • 95
  • 4
  • 5
    What is the desired result? You may just want `split(cars, cars$speed)` – Rich Scriven Oct 21 '15 at 18:34
  • I'm trying to create different objects depending on car$speed so that I can create a frequency plot for each object. So I will have an object that only contains cars$speed ==4, and I will make a frequency plot of cars$dist just for cars$speed==4. So I will make a number of different plots and the data will be divided by cars$speed - does that even make sense?!? – Lee12345 Oct 21 '15 at 18:37
  • do you want `cars[cars$speed == 4,]`, or `subset(cars,cars$speed == 4)`? see `?subset` or `?\`[\`` for details – Jthorpe Oct 21 '15 at 18:45
  • Have a look at `facet_wrap` or `facet_grid` from the `ggplot` package. – Jaap Oct 21 '15 at 18:47
  • 1
    Furthermore: Read the info about how to give a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Oct 21 '15 at 18:50
  • Also, you should look at `?apply` because the error you're getting specifically tells you why your current approach isn't working. – tblznbits Oct 21 '15 at 19:23

1 Answers1

0

A correct syntax for your apply function would be:

lapply(unique(cars$speed), function(xx) subset(cars, speed==xx))

Where you have a dataset followed by the function you want to apply. This is why you keep getting the error telling you that FUN is missing.

@RichardScriven 's split(cars, cars$speed) is a much more elegant solution however.

If you want to plot the frequency of dist for each speed you could do something like this:

library(ggplot2)
ggplot(cars, aes(dist)) +
  geom_bar() +
  facet_wrap(~speed)
DunderChief
  • 726
  • 4
  • 7