[R] Split dataframe into new dataframes

538280 at gmail.com 538280 at gmail.com
Fri Feb 10 18:21:51 CET 2012


I think one of your problems (the others have been addressed by
others) is that you want the expression x$y to represent a column of x
whose name is stored in y (not the name y itself).  The problem here
is that the $ notation is a magical shortcut and like any other magic
if used incorrectly is likely to do the programmatic equivalent of
turning yourself into a toad.  It is better to use '[[' instead of the
shortcut, try replacing x$y with x[[y]] and see if that fixes that
problem.

On Wed, Feb 8, 2012 at 2:11 PM, Johannes Radinger <JRadinger at gmx.at> wrote:
> Hi,
>
> I want to split a dataframe based on a grouping variable (in one column). The resulting new
> dataframes should be stored in a new variable. I tried to split the dataframe using split() and
> to store it using a FOR loop, but thats not working so far:
>
> df <- data.frame(A=c("A1","A1","A2","A2"),B=seq(1:4))
>
> Fsplit <- function(x,y){
>        ls <- split(x,f=x$y)
>        for (i in names(ls)){
>                i <- ls$i
>        }
> }
>
> Fsplit(df,A) #1st argument is dataframe to split, 2nd argument grouping variable
>
>
> Any suggestions how to get that done?
>
> Best regards
> Johannes
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list