[R] Help with dplyr

Axel Urbiz axel.urbiz at gmail.com
Fri Nov 6 03:26:03 CET 2015


Thank you all!


> On Nov 5, 2015, at 9:07 PM, William Dunlap <wdunlap at tibco.com> wrote:
> 
> Did you mean to add stringsAsFactors=FALSE to the following call to
> data.frame?
>   bin <- data.frame(
>      pred = pred,
>      bin = cut(pred, breaks = Breaks, include.lowest = TRUE))
> Since cut() produces a factor you would also have to convert it to character
> to make stringAsFactors=FALSE to have an effect.
>   bin <- data.frame(stringsAsFactors=FALSE,
>      pred = pred,
>      bin = as.character(cut(pred, breaks = Breaks, include.lowest = TRUE)))
> 
> However, I suspect that things would work out better if all the calls to
> cut used the same breaks and then leaving it as a factor would be fine.
> 
> 
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
> 
> On Thu, Nov 5, 2015 at 5:45 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
> wrote:
> 
>> Yes,  that was my intention, but it appears I may not have read his code
>> carefully enough.
>> ---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>> Go...
>>                                      Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>> 
>> On November 5, 2015 5:23:38 PM PST, David Winsemius <
>> dwinsemius at comcast.net> wrote:
>>> 
>>>> On Nov 5, 2015, at 4:58 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
>>> wrote:
>>>> 
>>>> Solution is to always use the stringsAsFactors=TRUE option in your
>>> data.frame() function calls.
>>> 
>>> Since that is the default, I’m wondering if you meant to say FALSE?
>>> 
>>>>>> David.
>>>> 
>> 
>>> ---------------------------------------------------------------------------
>>>> Jeff Newmiller                        The     .....       .....  Go
>>> Live...
>>>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>>> Go...
>>>>                                     Live:   OO#.. Dead: OO#..
>>> Playing
>>>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>>>> /Software/Embedded Controllers)               .OO#.       .OO#.
>>> rocks...1k
>>>> 
>> 
>>> ---------------------------------------------------------------------------
>>> 
>>>> Sent from my phone. Please excuse my brevity.
>>>> 
>>>> On November 5, 2015 3:59:10 PM PST, Axel Urbiz <axel.urbiz at gmail.com>
>>> wrote:
>>>>> Hello,
>>>>> 
>>>>> Is there a way to avoid the warning below in dplyr. I’m performing
>>> an
>>>>> operation within groups, and the warning says that the factors
>>> created
>>>>> from each group do not have the same levels, and so it coerces the
>>>>> factor to character. I’m using this inside a package I’m developing.
>>>>> I’d appreciate your recommendation on how to handle this.
>>>>> 
>>>>> library(dplyr)
>>>>> 
>>>>> set.seed(4)
>>>>> df <- data.frame(pred = rnorm(100), models = gl(2, 50, 100, labels =
>>>>> c("model1", "model2")))
>>>>> 
>>>>> create_bins <- function (pred, nBins) {
>>>>> Breaks <- unique(quantile(pred, probs = seq(0, 1, 1/nBins)))
>>>>> bin <- data.frame(pred = pred, bin = cut(pred, breaks = Breaks,
>>>>> include.lowest = TRUE))
>>>>> bin
>>>>> }
>>>>> 
>>>>> res_dplyr <- df %>% group_by(models) %>% do(create_bins(.$pred, 10))
>>>>> Warning message:
>>>>> In rbind_all(out[[1]]) : Unequal factor levels: coercing to
>>> character
>>>>> 
>>>>> Thank you,
>>>>> Axel.
>>>>> 
>>>>> 
>>>>>    [[alternative HTML version deleted]]
>>>>> 
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> 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.
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>> 
>>> David Winsemius
>>> Alameda, CA, USA
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list