简体   繁体   中英

R - Passing data frame to function

R beginner here. I'm trying to write a function on my own which has a data frame as an argument and then reorders the data frame and then uses ggplot. I've been struggling with trying to get the function to work and somehow I can't seem to find the answer I'm looking for.

The first code I had was this,

pareto_plot <- function(pareto_data, title, x_label, y_label, filename){
pareto_calc = pareto_data[order(-pareto_data[2]),]
colnames(pareto_calc) = c("sku", "volume")

pareto_calc$sku_perc = 1/length(pareto_calc$sku)
pareto_calc$sku_cum = cumsum(pareto_calc$sku_perc)

pareto_calc$vol_perc = pareto_calc$volume/sum(pareto_calc$volume)
pareto_calc$vol_cum = cumsum(pareto_calc$vol_perc)

ggplot(pareto_calc, aes(x=pareto_data$sku_cum, y=pareto_data$vol_cum)) +   geom_line(col="blue") + 
geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") + 
ggtitle(title) + ylab(y_label) + xlab(x_label)

ggsave(paste(filename,".png", sep=""))
}

When I used the above code, I got an error,

Error in eval(expr, envir, enclos) : object 'pareto_calc' not found

I then changed the code to make use of data as i saw that a lot of examples online made use of it as an argument. My modified code was now,

pareto_plot <- function(data, title, x_label, y_label, filename){
pareto_data = data

pareto_data[order(-pareto_data[2]),]
colnames(pareto_data) = c("sku", "volume")

pareto_data$sku_perc = 1/length(pareto_data$sku)
pareto_data$sku_cum = cumsum(pareto_data$sku_perc)

pareto_data$vol_perc = pareto_data$volume/sum(pareto_data$volume)
pareto_data$vol_cum = cumsum(pareto_data$vol_perc)

ggplot(pareto_data, aes(x=pareto_data$sku_cum, y=pareto_data$vol_cum)) +   geom_line(col="blue") + 
geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") + 
ggtitle(title) + ylab(y_label) + xlab(x_label)

ggsave(paste(filename,".png", sep=""))
}

With this code, I now get the error,

Error in exists(name, envir = env, mode = mode) : 
argument "env" is missing, with no default

Any help will be greatly appreciated. Thanks in advance! :)

When you make a function, it is often easiest to write the code first, without making it a function, until you are sure it works. Then wrap it as a function.

set.seed(33)

df <- data.frame(V1 = runif(10),
                 V2 = rnorm(10))

pareto_plot <- function(data, title, x_label, y_label, filename){
        pareto_data <- data[order(-data[2]),] #you forgot to assign it

        names(pareto_data)  <-  c("sku", "volume")

        pareto_data$sku_perc  <-  1/length(pareto_data$sku)
        pareto_data$sku_cum <- cumsum(pareto_data$sku_perc)

        pareto_data$vol_perc <- pareto_data$volume/sum(pareto_data$volume)
        pareto_data$vol_cum <- cumsum(pareto_data$vol_perc)

        ggplot(pareto_data, aes(x=sku_cum, y=vol_cum)) +   geom_line(color="blue") + 
                geom_line(y=0.8, col="red") +geom_line(x=0.2, col="red") + 
                ggtitle(title) + ylab(y_label) + xlab(x_label)

        ggsave(paste(filename,".png", sep=""))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM