简体   繁体   English

如何创建返回给定数据框直方图的 function?

[英]How do I create a function that returns the histogram of a given data frame?

I need to create a function called histagram, the function receives 3 arguments: data frame, column name, and color number.我需要创建一个名为 histagram 的 function,function 接收 3 arguments:数据框、列名和颜色编号。

if all the arguments meet the criteria (valid data frame, color number an integer, and column name input must be character and the column must be of type numeric, the function returns a plot of histogram of the chosen column with the chosen color.如果所有 arguments 都满足条件(有效数据框,颜色编号 integer,并且列名输入必须是字符且列必须是数字类型,则 function 返回具有所选颜色的所选列的直方图 plot。

Also we need to change the "x - axis" so it will represent the column name input.我们还需要更改“x - 轴”,以便它代表列名输入。

my script:我的脚本:

histagram = function(dataframe, columnName, colorNumber) {
    if (!is.data.frame(dataframe)){
    print("Please enter a Data frame structure")
  } else if(!is.integer(as.integer(colorNumber))){
    print("Enter a char columne name")    
  } else if(!is.character(columnName)){
    print("Color must be a integer")  
  } else {
    return(hist(dataframe$as.numeric(columnName), xlab = "columnName", col = as.integer(colorNumber)))
  }
} 

I tested the function to see if it worked:我测试了 function 看它是否有效:

histagram(dataframe = airdf, colorNumber = 7, columnName = "Temp")

I used the airquality data frame, which is a built-in data frame in R. airdf = airquality.我用的是airquality数据框,是R中内置的数据框。airdf = airquality。

I'm getting an error message -我收到一条错误消息 -

Error in dataframe$as.numeric(columnName) : attempt to apply non-function
Called from: hist(dataframe$as.numeric(columnName), xlab = "columnName", col = as.integer(colorNumber))

What needs to be changed in my code for it to work?我的代码需要更改哪些内容才能正常工作?

Many thanks!非常感谢!

In addition to Martin Gal's comment, you should also remove the quotations around columnName under the xlab argument and consider adding something to the histogram title.除了 Martin Gal 的评论外,您还应该删除xlab参数下columnName周围的引号,并考虑在直方图标题中添加一些内容。 If you don't specify the histogram title, it just says "Histogram of as.numeric(dataframe[, columnName])", which is a bit clunky.如果您不指定直方图标题,它只会显示“Histogram of as.numeric(dataframe[, columnName])”,这有点笨拙。

data(airquality)
airdf <- airquality

histagram <- function(dataframe, columnName, colorNumber) {
  if (!is.data.frame(dataframe)){
    print("Please enter a Data frame structure")
  } else if(!is.integer(as.integer(colorNumber))){
    print("Enter a char columne name")    
  } else if(!is.character(columnName)){
    print("Color must be a integer")  
  } else {
    return(hist(as.numeric(dataframe[, columnName]), # <- from Martin Gal
                main = paste("Histogram of", columnName), # <- dynamic title 
                xlab = columnName, # <- remove quotes here
                col = as.integer(colorNumber)))
  }
} 

histagram(dataframe = airdf, colorNumber = 7, columnName = "Temp")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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