简体   繁体   中英

function argument definition issue in R

Here is my problem, when I want to develop a function for widespread use.

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

I would like to define using the first type of definition, where variable name argument are in "parenthesis" instead of "datafile$variablename"

You simply need to not use the $ as a column selector. Instead, use [ :

dataframe[,A]

or

dataframe[,B]

and so on. In your case:

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

The direct answer to your question is to use [ indexing rather than $ indexing, since $ indexing doesn't allow indirect subsetting.

Furthermore, you can substantially improve the readability of your function by using a single data.frame statement without defining any intermediate variables:

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

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