I need to create a function called histagram, the function receives 3 arguments: data frame, column name, and color number.
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.
Also we need to change the "x - axis" so it will represent the column name input.
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:
histagram(dataframe = airdf, colorNumber = 7, columnName = "Temp")
I used the airquality data frame, which is a built-in data frame in 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!
1条答案
按热度按时间slwdgvem1#
除了Martin Gal的注解之外,您还应该删除
xlab
参数下columnName
周围的引号,并考虑在直方图标题中添加一些内容。如果您不指定直方图标题,它只显示“Histogram of as.numeric(dataframe[,columnName])",这有点笨拙。