简体   繁体   English

我的函数中的循环不会改变我的数据类型(在 R 中)

[英]The Loop in My Function does not change my data type (in R)

I intend to use a function to save me typing work for repetitive procedures.我打算使用一个函数来节省重复程序的打字工作。 Many things are already working but not everything is working yet.许多事情已经在起作用,但并非一切都在起作用。 Here is the code:这是代码:

quicky <- function(df, factors){
      output <- as.character(substitute(factors)[-1])
      print(output)
      df[,output]
      for(i in names(df[,output])){
        hist(df[,as.character(i)])
        df[,as.character(i)] <- as.factor(df[,as.character(i)])#<- Why does this not work?
      }
    }

quicky(mtcars, c(cyl,hp,drat))

Request for help and explanation!请求帮助和解释! Thanks in advance.提前致谢。

As we are looping over the column names created from 'output', just looping over those instead of further subsetting the data and getting te names .当我们遍历从 'output' 创建的列名时,只需遍历这些列名,而不是进一步对数据进行子集化并获取 te names Also, in the function, return the dataset at the end另外,在函数中,最后return数据集

quicky <- function(df, factors){
      output <- as.character(substitute(factors)[-1])
      print(output)
     for(i in output){               
          df[[i]] <- as.factor(df[[i]])
           }

           df

    }

out <- quicky(mtcars, c(cyl,hp,drat))
str(out)
#'data.frame':  32 obs. of  11 variables:
# $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
# $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...  ###
# $ disp: num  160 160 108 258 360 ...
# $ hp  : Factor w/ 22 levels "52","62","65",..: 11 11 6 11 15 9 20 2 7 13 ...###
# $ drat: Factor w/ 22 levels "2.76","2.93",..: 16 16 15 5 6 1 7 11 17 17 ...###
# $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
# $ qsec: num  16.5 17 18.6 19.4 17 ...
# $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
# $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
# $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
# $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

NOTE: Changed the [ to [[ so that it works with data.table and tbl_df注意:将[更改为[[以便它适用于data.tabletbl_df

The reason quickly is failing to return the results of assignments to the columns of df is a peculiar feature of an R for -loop. quickly的原因是未能将分配结果返回到df的列是 R for循环的一个特殊功能。 It returns NULL.它返回NULL。 And the last function that was evaluated within your quicky function was for .在您的quicky函数中评估的最后一个函数是for So all you need to do is add a call to the value of df outside of the loop:因此,您需要做的就是在循环外添加对df值的调用:

quicky <- function(df, factors){
    output <- as.character(substitute(factors)[-1])
    print(output)
    df[,output]
    for(i in names(df[,output])){
        hist(df[,as.character(i)])
        df[, i] <- as.factor(df[, i ])
    }; df  # add a call to evaluate `df` 
}

str( quicky(mtcars, c(cyl,hp,drat)) )
#-------
[1] "cyl"  "hp"   "drat"
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : Factor w/ 22 levels "52","62","65",..: 11 11 6 11 15 9 20 2 7 13 ...
 $ drat: Factor w/ 22 levels "2.76","2.93",..: 16 16 15 5 6 1 7 11 17 17 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ..

This behavior of for is in contrast to most other functions in R. With a for-loop, the evaluations and assignments done within it typically become effective outside the for -loop body, ie in the calling environment, but the function call itself returns NULL.这种行为for形成对比,在R.大多数其他功能有了一个for循环,评价和内完成它的任务通常生效外for -loop体,即在调用环境,但该函数调用自身返回NULL . Most other functions have no effect outside their function body environments which then requires the programmer to assign the returned value to a named object if any lasting effect is desired.大多数其他函数在它们的函数体环境之外没有任何效果,如果需要任何持久效果,则需要程序员将返回值分配给命名对象。 (You should, of course, not expect the value of mtcars to be affected by that action.) (当然,您不应该期望mtcars的价值会受到该操作的影响。)

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

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