Illya Mowerman
Example:
Example:
# Error message
Error in summarise(grouped_df(df, groups), mean_value = mean(value)) :
object 'value' not found
# AI suggestion
# The error suggests that the column 'value' doesn't exist in your dataframe.
# Check your column names and ensure you're using the correct name:
df %>%
group_by(category) %>%
summarise(mean_value = mean(correct_column_name, na.rm = TRUE))Example:
# AI-generated code for a complex ggplot visualization
ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl), size = wt)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~gear) +
scale_color_viridis_d() +
theme_minimal() +
labs(title = "Car Performance Metrics",
x = "Miles per Gallon",
y = "Horsepower",
color = "Cylinders",
size = "Weight")Example:
# AI recommendation for time series forecasting
"For time series forecasting in R, consider using the 'forecast' package:
install.packages('forecast')
library(forecast)
# Example usage:
data(AirPassengers)
model <- auto.arima(AirPassengers)
forecast(model, h = 12)
# The 'forecast' package offers robust algorithms and is widely used in the R community for time series analysis."Example:
#' Calculate the Weighted Moving Average
#'
#' This function computes the weighted moving average of a given vector.
#'
#' @param x A numeric vector of values.
#' @param weights A numeric vector of weights. Must have the same length as the window size.
#' @param k The size of the moving window.
#'
#' @return A numeric vector of the same length as x, containing the weighted moving averages.
#'
#' @examples
#' data <- c(1, 2, 3, 4, 5)
#' weights <- c(0.1, 0.2, 0.7)
#' weighted_ma(data, weights, 3)
#'
#' @export
weighted_ma <- function(x, weights, k) {
# Function implementation here
}Example:
Example:
"Challenge: Create a function that takes a dataframe and returns a list of summary statistics for each numeric column. Use the purrr package for functional programming.
Hint: Start with something like this:
summarize_numeric <- function(df) {
df %>%
select(where(is.numeric)) %>%
map(~ list(
mean = mean(.x, na.rm = TRUE),
median = median(.x, na.rm = TRUE),
sd = sd(.x, na.rm = TRUE)
))
}
Try implementing this function and test it on the mtcars dataset!"Example:
# AI-generated skeleton for a simple Shiny app
library(shiny)
ui <- fluidPage(
titlePanel("My Shiny App"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Choose a variable:",
choices = names(mtcars))
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(mtcars, aes_string(x = input$variable)) +
geom_histogram(bins = 30) +
theme_minimal()
})
}
shinyApp(ui = ui, server = server)Example: