简体   繁体   中英

Using lapply in group_by() of two factors in R

I have this data frame (named as OEM_final ). This is the structure:

str(OEM_final)
'data.frame':   2265 obs. of  17 variables:
 $ dia_hora_OEM : POSIXct, format: "2019-12-31 06:40:13" "2019-12-31 06:43:00" "2019-12-31 07:11:30" "2019-12-31 07:18:30" ...
 $ coche_OEM    : Factor w/ 6 levels "356232050832996",..: 3 3 3 3 3 3 3 3 6 6 ...
 $ DTC_OEM_dec64: chr  "[{\"code\":\"B1182\",\"description\":\"Tire pressure monitor module\",\"faultInformations\":[{\"description\":\"| __truncated__ "[{\"code\":\"B1182\",\"description\":\"Tire pressure monitor module\",\"faultInformations\":[{\"description\":\"| __truncated__ "[{\"code\":\"B1182\",\"description\":\"Tire pressure monitor module\",\"faultInformations\":[{\"description\":\"| __truncated__ "[{\"code\":\"B1182\",\"description\":\"Tire pressure monitor module\",\"faultInformations\":[{\"description\":\"| __truncated__ ...
 $ rowname      : Factor w/ 2265 levels "1","10","100",..: 1 1112 1489 1600 1711 1822 1933 2044 2155 2 ...
 $ B1182        : Factor w/ 2 levels "B1182","NULL": 1 1 1 1 1 1 1 1 2 2 ...
 $ B124D        : Factor w/ 2 levels "B124D","NULL": 1 1 1 1 1 1 1 1 2 2 ...
 $ NA.          : Factor w/ 6 levels "c(NA, NA, NA, NA, NA, NA, NA, NA)",..: 3 3 3 3 3 3 3 3 1 1 ...
 $ P2000        : Factor w/ 2 levels "c(\"P2000\", \"P2000\", \"P2000\")",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ U3003        : Factor w/ 2 levels "NULL","U3003": 1 1 1 1 1 1 1 1 1 1 ...
 $ B1D01        : Factor w/ 3 levels "B1D01","c(\"B1D01\", \"B1D01\")",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ U0155        : Factor w/ 2 levels "NULL","U0155": 1 1 1 1 1 1 1 1 1 1 ...
 $ C1B00        : Factor w/ 2 levels "C1B00","NULL": 2 2 2 2 2 2 2 2 2 2 ...
 $ P037D        : Factor w/ 2 levels "NULL","P037D": 1 1 1 1 1 1 1 1 1 1 ...
 $ P0616        : Factor w/ 2 levels "NULL","P0616": 1 1 1 1 1 1 1 1 1 1 ...
 $ P0562        : Factor w/ 2 levels "NULL","P0562": 1 1 1 1 1 1 1 1 1 1 ...
 $ U0073        : Factor w/ 2 levels "NULL","U0073": 1 1 1 1 1 1 1 1 1 1 ...
 $ P0138        : Factor w/ 2 levels "c(\"P0138\", \"P0138\", \"P0138\")",..: 2 2 2 2 2 2 2 2 2 2 ...

I would like to calculate the earlier date ( dia_hora_OEM ) that appears when grouping by two factors. The two factors are:

  • One of this factor, which is common in all the possible combinations, is coche_OEM .
  • The other one is one from column 8 ( P2000 ) to the last one ( P0138 ), one at a time.

So, the group_by() would be:

  • group_by(coche_OEM, P2000)
  • group_by(coche_OEM, U3003)
  • group_by(coche_OEM, B1D01)
  • group_by(coche_OEM, U0155)
  • ...

I tried different ways to accomplish this:

Using for loops:

for (DTC in c(U3003, P2000)) {
  OEM_final %>%
  group_by(DTC, coche_OEM) %>%
  filter(dia_hora_OEM == min(dia_hora_OEM))
}

But I get an error saying:

Error in c(U3003, P2000) : object 'U3003' not found

Using lapply

In this case, I created a function:

groupCombDTC <- function(x) {
  OEM_final %>%
  group_by(coche_OEM, x) %>%
  filter(dia_hora_OEM == min(dia_hora_OEM))
}

And then I ran lapply() :

lapply(colnames(OEM_final)[8:17], groupCombDTC)

I get this error:

Error: Column `x` is unknown

Can anybody help me iterating in different combinations using group_by() ?

That's a standard problem of standard evaluation with dplyr . dplyr is based on non standard evaluation so quoted arguments need to be unquoted.

Several solutions exist. This one works well

groupCombDTC <- function(x) {
  OEM_final %>%
  group_by(coche_OEM, !!rlang::sym(x)) %>%
  filter(dia_hora_OEM == min(dia_hora_OEM))
}

It requires to use together !! and rlang::sym to unquote and evaluate your variable name.

Column names as arguments are easier to handle with data.table . If you want more elements regarding SE/NSE in dplyr and data.table , you can have a look at a blog post I wrote a few days ago

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