Am 08.02.2012 um 22:19 schrieb David Winsemius:
>
> On Feb 8, 2012, at 4:11 PM, Johannes Radinger 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
>>
>
> It appears you want the name of the levels of df$A to be the names of
> separate variables in the global environment. If that is correct, then see
> the FAQ. I'm not sure which one it is among the Miscellaneous section, but
> you should be looking of the one that tells you how to construct a named
> variable.
>
Your hint with the global environment brought me on track. It seems that I this
task can be done with list2env() although there is still a problem with my
function. How
can I parse the name of the dataframe and the column name in the function...
df <- data.frame(A=c("A1","A1","A2","A2"),B=seq(1:4))
Fsplit <- function(x,y){
ls <- split(x,f=x$y)
list2env(ls,envir = .GlobalEnv)
}
Fsplit(df,A)
/johannes
> Or:
>
> ? assign
>
> --
> David Winsemius, MD
> West Hartford, CT
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.