# NOT RUN {
# }
# NOT RUN {
# Computes the arithmetic mean of the second column by grouping
# on the first and third columns. Output the grouping values and the average.
df <- createDataFrame (
list(list(1L, 1, "1", 0.1), list(1L, 2, "1", 0.2), list(3L, 3, "3", 0.3)),
c("a", "b", "c", "d"))
result <- gapplyCollect(
df,
c("a", "c"),
function(key, x) {
y <- data.frame(key, mean(x$b), stringsAsFactors = FALSE)
colnames(y) <- c("key_a", "key_c", "mean_b")
y
})
# We can also group the data and afterwards call gapply on GroupedData.
# For example:
gdf <- group_by(df, "a", "c")
result <- gapplyCollect(
gdf,
function(key, x) {
y <- data.frame(key, mean(x$b), stringsAsFactors = FALSE)
colnames(y) <- c("key_a", "key_c", "mean_b")
y
})
# Result
# ------
# key_a key_c mean_b
# 3 3 3.0
# 1 1 1.5
# Fits linear models on iris dataset by grouping on the 'Species' column and
# using 'Sepal_Length' as a target variable, 'Sepal_Width', 'Petal_Length'
# and 'Petal_Width' as training features.
df <- createDataFrame (iris)
result <- gapplyCollect(
df,
df$"Species",
function(key, x) {
m <- suppressWarnings(lm(Sepal_Length ~
Sepal_Width + Petal_Length + Petal_Width, x))
data.frame(t(coef(m)))
})
# Result
# ---------
# Model X.Intercept. Sepal_Width Petal_Length Petal_Width
# 1 0.699883 0.3303370 0.9455356 -0.1697527
# 2 1.895540 0.3868576 0.9083370 -0.6792238
# 3 2.351890 0.6548350 0.2375602 0.2521257
# }
Run the code above in your browser using DataLab