R中的函数参数定义问题

时间:2011-11-19 16:07:01

标签: r function arguments

这是我的问题,当我想开发广泛使用的功能时。

dataframe1 <- data.frame(V1 = 1:10, V2 = 11:20, V3 = 21:30, V4 = 31:40)
myfun <- function (dataframe, A, B, yvar) {
                  dataframe1$A <- as.factor(dataframe$A)
                  dataframe1$B <- as.factor (dataframe$B)
                  dataframe1 <- data.frame(A = dataframe1$A, 
                  B = dataframe1$B, yvar = dataframe1$yvar)
                  print(dataframe1)
                  }

 myfun (dataframe = dataframe1, A = "V1", B= "V2", yvar = "V3")
 Error in `$<-.data.frame`(`*tmp*`, "A", value = integer(0)) :
  replacement has 0 rows, data has 10

myfun (dataframe = dataframe1, A = dataframe1$V1, 
         B= dataframe1$V2, yvar = dataframe1$V3)
Error in `$<-.data.frame`(`*tmp*`, "A", value = integer(0)) :
  replacement has 0 rows, data has 10

我想使用第一种类型的定义来定义,其中变量名称参数在“括号”而不是“datafile $ variablename”

2 个答案:

答案 0 :(得分:6)

您只需要不使用$作为列选择器。相反,请使用[

dataframe[,A]

dataframe[,B]

等等。在你的情况下:

dataframe1 <- data.frame(V1 = 1:10, V2 = 11:20, V3 = 21:30, V4 = 31:40)
myfun <- function (dataframe, A, B, yvar) {
                   dataframe1[,A] <- as.factor(dataframe[,A])
                   dataframe1[,B] <- as.factor (dataframe[,B])
                   dataframe1 <- data.frame(A = dataframe1[,A], 
                   B = dataframe1[,B], yvar = dataframe1[,yvar])
                   print(dataframe1)
                   }

myfun (dataframe = dataframe1, A = "V1", B= "V2", yvar = "V3")

    A  B yvar
1   1 11   21
2   2 12   22
3   3 13   23
4   4 14   24
5   5 15   25
6   6 16   26
7   7 17   27
8   8 18   28
9   9 19   29
10 10 20   30

答案 1 :(得分:3)

您问题的直接答案是使用[索引而不是$索引,因为$索引不允许间接子集化。

此外,您可以使用单个data.frame语句大幅提高函数的可读性,而无需定义任何中间变量:

myfun <- function (dataframe, A, B, yvar) {
  data.frame(
      A = as.factor(dataframe[, A]), 
      B = as.factor(dataframe[, B]),
      yvar = dataframe[, yvar]
  )
}

myfun(dataframe1, "V1", "V2", "V3")
    A  B yvar
1   1 11   21
2   2 12   22
3   3 13   23
4   4 14   24
5   5 15   25
6   6 16   26
7   7 17   27
8   8 18   28
9   9 19   29
10 10 20   30