将数据框列传递给函数错误

时间:2017-08-24 10:07:42

标签: r function

我正在编写一个函数并将数据框的列传递给函数。

您可以找到数据here

var aryData = [ "20170101|9,4,4,2,6", "20170102|0,7,5,6,2"]
   ,aryObjects = [];
for( var i in aryData ) {
    var aryFields = aryData[i].split("|");

    if ( aryFields.length != 2 ) {
        continue;
    }
    var aryNumbers = aryFields[1].split(","), intSum = 0;

    for( var n in aryNumbers ) {
        aryNumbers[n] = parseInt(aryNumbers[n]);
        intSum += aryNumbers[n];
    }
    aryObjects.push({date:aryFields[0]
                 ,numbers:aryNumbers
                     ,sum:intSum
               ,oddorEven:(((intSum % 2) == 0) ? "even" : "odd")});
}
console.dir(aryObjects);
  

hist.default出错(table_name $ column_name,breaks = 10,col =“lightgray”):'x'必须是数字

注意:没有功能,这可以正常工作。

1 个答案:

答案 0 :(得分:0)

您无法调用table_output$column_name来对表格进行子集化。尝试:

normallity_plot_func <- function (table_name, column_name) {
  histogram <-
    hist(table_name[, column_name],
         breaks = 10,
         col = "lightgray",
         xlab = "a")
  xfit <-
    seq(min(table_name[, column_name]), max(table_name[, column_name]), length = 40)
  yfit <-
    dnorm(xfit,
          mean = mean(table_name[, column_name]),
          sd = sd(table_name[, column_name]))
  yfit <-
    yfit * diff(histogram$mids[1:2]) * length(table_name[, column_name])
  lines(xfit, yfit, col = "black", lwd = 2)
}

normallity_plot_func(iris, "Sepal.Length")
Risk <- read.delim("~/SE/Risk.txt")
normallity_plot_func(Risk,"INCOME")

enter image description here 我所做的就是将table_output$column_name更改为table_output[,column_name]