[R] Subset() within function: logical error
Rich Shepard
rshepard at appl-ecosys.com
Tue Jun 30 04:18:45 CEST 2015
On Tue, 30 Jun 2015, Rolf Turner wrote:
> If you want a pointer to the correct syntax for subset(), try
> help("subset")!!!
>
> The syntax of your "extstream" function is totally screwed up, convoluted and
> over-complicated. Note that even if you had your "subset" argument specified
> correctly, the return() call will give you only the result from the *first*
> pass through the for loop.
>
> That aside, the error message is perfectly clear: 'subset' must be logical.
> Your "subset" argument is "stream" which is a factor.
>
> You *could* redefine your "extstream" function as follows:
>
> function(alldf) {
> sname <- levels(alldf$stream)
> rslt <- vector("list",length(sname))
> names(rslt) <- sname
> for (i in sname) {
> rslt[[i]] <- subset(alldf, alldf$stream==i, sampdate:quant)
> }
> rslt
> }
>
> However you don't need to go through such contortions:
>
> split(testset,testset$stream)
>
> will give essentially what you want. If you wish to strip out the redundant
> "stream" column from the data frames in the resulting list, you could do that
> using lapply()
Rolf,
I did re-read the subset man page, but did not associate the error message
with the problem.
Thanks very much for the lesson. I will read the split() man page; simple
is always better.
Regards,
Rich
More information about the R-help
mailing list