Github user shivaram commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11569#discussion_r60797789
  
    --- Diff: R/pkg/R/DataFrame.R ---
    @@ -2465,6 +2465,110 @@ setMethod("drop",
                 base::drop(x)
               })
     
    +#' This function computes a histogram for a given SparkR Column.
    +#' 
    +#' @name histogram
    +#' @title Histogram
    +#' @param nbins the number of bins (optional). Default value is 10.
    +#' @param df the DataFrame containing the Column to build the histogram 
from.
    +#' @param colname the name of the column to build the histogram from.
    +#' @return a data.frame with the histogram statistics, i.e., counts and 
centroids.
    +#' @rdname histogram
    +#' @family DataFrame functions
    +#' @export
    +#' @examples 
    +#' \dontrun{
    +#' # Create a DataFrame from the Iris dataset
    +#' irisDF <- createDataFrame(sqlContext, iris)
    +#' 
    +#' # Compute histogram statistics
    +#' histData <- histogram(df, "colname"Sepal_Length", nbins = 12)
    +#'
    +#' # Once SparkR has computed the histogram statistics, the histogram can 
be
    +#' # rendered using the ggplot2 library:
    +#'
    +#' require(ggplot2)
    +#' plot <- ggplot(histStats, aes(x = centroids, y = counts))
    +#' plot <- plot + geom_histogram(data = histStats, stat = "identity", 
binwidth = 100)
    +#' plot <- plot + xlab("Sepal_Length") + ylab("Frequency")   
    +#' } 
    +setMethod("histogram",
    +          signature(df = "DataFrame", col = "characterOrColumn"),
    +          function(df, col, nbins = 10) {
    +            # Validate nbins
    +            if (nbins < 2) {
    +              stop("The number of bins must be a positive integer number 
greater than 1.")
    +            }
    +
    +            # Round nbins to the smallest integer
    +            nbins <- floor(nbins)
    +
    +            # Validate col
    +            if (is.null(col)) {
    +              stop("col must be specified.")
    +            }
    +
    +            colname <- col
    +            x <- if (class(col) == "character") {
    +              if (!colname %in% names(df)) {
    +                stop("Specified colname does not belong to the given 
DataFrame.")
    +              }
    +
    +              # Filter NA values in the target column and remove all other 
columns
    +              df <- na.omit(df[, colname])
    +              getColumn(df, colname)
    +
    +            } else if (class(col) == "Column") {
    +
    +              # Append the given column to the dataset. This is to support 
Columns that
    +              # don't belong to the DataFrame but are rather expressions
    +              df$x <- col
    --- End diff --
    
    Do we need to check if `x` is a column name already present in the data 
frame ? For example I ran the code
    ```
    irisDF$x <- irisDF$Petal_Width + 2.0
    histogram(irisDF, irisDF$x, 8)
    ```
    and I got an error
    ```
    org.apache.spark.sql.AnalysisException: resolved attribute(s) x#141 missing 
from Species#4,Sepal_Length#0,x#269,Petal_Width#3,Petal_Length#2,Sepal_Width#1 
in operator !Project 
[Sepal_Length#0,Sepal_Width#1,Petal_Length#2,Petal_Width#3,Species#4,x#269,cast((((cast(cast((((x#141
 - 2.1) / 2.4) * 10000.0) as int) as double) / 10000.0) / 0.125) - CASE WHEN 
((((cast(cast((((x#141 - 2.1) / 2.4) * 10000.0) as int) as double) / 10000.0) / 
0.125) = cast(cast(((cast(cast((((x#141 - 2.1) / 2.4) * 10000.0) as int) as 
double) / 10000.0) / 0.125) as int) as double)) && NOT (x#141 = 2.1)) THEN 1.0 
ELSE 0.0 END) as int) AS bins#325]
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to