I have a dataset (insti) and I want to create 3 different subsets according to a factor (xarxa) with three levels (linkedin, instagram, twitter). I used this:
linkedin <- subset(insti, insti$xarxa=="linkedin")
twitter <- subset(insti, insti$xarxa=="twitter")
instagram <- subset(insti, insti$xarxa=="instagram")
It does work, however, I was wondering if this can be done with tapply, so I tried:
tapply(insti, insti$xarxa, subset)
It gives this error:
Error in tapply(insti, insti$xarxa, subset) : arguments must have same length
I think that there might be some straigth forward way to do this but I can not work it out. Can you help me with this without using loops? Thanks a lot.
It's usually better to deal with data frames in a named list. This makes them easy to iterate over, and stops your global workspace being filled up with lots of different variables. The easiest way to get a named list is with
split(insti, insti$xarxa).If you really want the variables written directly to your global environment rather than in a list with a single line, you can do
Example
Obviously, I don't have the
instidata frame, since you did not supply any example data in your question, but we can demonstrate that the above solution works using the built-inirisdata set.First we can see that my global environment is empty:
Now we get the
irisdata set, split it by species, and put the result in the global environment:So now when we check the global environment's contents, we can see that we have three data frames: one for each
Species:And of course, we can also access
versicolorandvirginicain the same wayCreated on 2021-11-12 by the reprex package (v2.0.0)