简体   繁体   中英

Function in R for several variables

I want to unclass several factor variables in R. I need this functionality for a lot of variables. At the moment I repeat the code for each variable which is not convenient:

unclass:
  myd$ati_1 <-unclass(myd$ati_1) 
  myd$ati_2 <-unclass(myd$ati_2)  
  myd$ati_3 <-unclass(myd$ati_3) 
  myd$ati_4 <-unclass(myd$ati_4)

I've looked into the apply() function family but I do not even know if this is the correct approach. I also read about for loops but every example is only about simple integers, not when you need to loop over several variables.

Would be glad if someone could help me out.

Here are a few ways. We use CO2 which comes with R and has several factor columns. This unclasses those columns.

If you need some other criterion then

  • set ix to the names or positions or a logical vector defining those columns to be transformed in the base R solution
  • replace is.factor in the collapse solution with a vector of names or positions or a logical vector denoting the columns to convert
  • in the dplyr solution replace where(...) with the same names, positions or logical.

Code follows. In all of these the input is not overridden so you still have the input available unchanged if you want to rerun it from scratch and, in general, overwriting objects is error prone.

# Base R
ix <- sapply(CO2, is.factor)
replace(CO2, ix, lapply(CO2[ix], unclass))

# collapse
library(collapse)
ftransformv(CO2, is.factor, unclass)

# dplyr
library(dplyr)
CO2 %>%
  mutate(across(where(is.factor), unclass))

Depending on what you want this might be sufficient or omit the as.data.frame if a matrix result is ok.

as.data.frame(data.matrix(CO2))

You can use a loop:

    block <- c("ati_1", "ati_2", "ati_3", "ati_4")
    for (j in block) {myd[[j]] <- unclass(myd[[j]])}
# The double brackets allows you to specify actual names to extrapolate withing the data frame

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