Roland has the correct solution for the specific problem: more than a split()
is not needed. Just to make sure: split()
returns a list. To get separate data frames in you workspace, you do:
list2env(split(df,df$b),.GlobalEnv)
Or, using assign:
tmp <- split(df,df$b)
for(i in names(tmp)) assign(i,tmp[[i]])
A word on subset
This said, some more detail on why your function is plain wrong. First of all, in ?subset
you read:
Warning
This is a convenience function intended for use interactively. For
programming it is better to use the standard subsetting functions like
[, and in particular the non-standard evaluation of argument subset
can have unanticipated consequences.
Translates to: Never ever in your life use subset()
within a function again.
A word on returning values from a function
Next to that, a function always returns a result:
- if a
return()
statement is used, it returns whatever is given as an argument to return()
.
- otherwise it returns the result of the last line.
In your case, the last line contains an assignment. Now an assignment also returns a value, but you don't see it. It's returned invisibly
. You can see it by wrapping it in parentheses, for example:
> x <- 10
> (x <- 20)
[1] 20
This is absolutely unnecessary. It's the reason why your function works when used in lapply()
(lapply catches invisible output), but won't give you any (visible) output when used at the command line. You can capture it though :
> testF("b")
> x <- testF("b")
> x
a b
3 3 b
6 6 b
10 10 b
The assignment in your function doesn't make sense: either you return dum
explicitly, or you just drop the assignment alltogether
Correcting your function
So, given this is just an example and the real problem wouldn't be solved by simply using split()
your function would be :
testF <- function(select) {
dum <- df[df$b=select,]
return(dum)
}
or simply:
testF <- function(select){
df[df$b=select,]
}