Home > OS >  Why would this dplyr mutate statement result in invalid 'length' argument error?
Why would this dplyr mutate statement result in invalid 'length' argument error?

Time:07-14

The below reproducible code works fine until it gets to the last mutate statement for ClassDecrease. It gives me the invalid 'length' argument error. What am I doing wrong? ClassDecrease should give me these results:

0
0
0
0
0
-1

and not this which it currently gives:

0
0
0
0
1
1

Reproducible code:

library(dplyr)

data <- 
  data.frame(
    Element = c("D","D","R","D","C","C"),
    SplitCode = c(0,0,0,0,1,1)
  )


Class_to_Count <- 1

excelCopy <- data %>% 
  group_by(Element) %>% 
  mutate(PreSplitClass = row_number()) %>% 
  ungroup() %>%
  mutate(ClassAtSplit =
           case_when(
             SplitCode == 0 ~ as.integer(0), # This eliminates checking for > 0
             SplitCode > lag(SplitCode) ~ PreSplitClass, # if > previous value
             SplitCode == lag(SplitCode) ~ lag(PreSplitClass) # if equal (0s are avoided)
           )
  ) %>%
  mutate(
    LowClassSplit =
      case_when(
        SplitCode > lag(SplitCode) & SplitCode == Class_to_Count ~ PreSplitClass,
        SplitCode == lag(SplitCode) & SplitCode == Class_to_Count ~ lag(PreSplitClass),
        TRUE ~ 0L
      )
  ) %>%
  mutate(
    GrossSubclass =
      case_when(
        LowClassSplit == Class_to_Count ~ PreSplitClass,
        TRUE ~ 0L
      )
    ) %>%
  group_by(Element) %>% 
  mutate(NetSubclass = cumsum(LowClassSplit == Class_to_Count)
         ) %>%
  
  ungroup %>%
  
  rowwise() %>% 
    mutate(
      ClassSubclass = sum(LowClassSplit, NetSubclass/10)
    ) %>%
  
  ungroup %>%
  
  mutate(ApplyToNoSplits = ClassSubclass[match(PreSplitClass, GrossSubclass)] * !SplitCode) %>%
  
  ungroup %>%
  
  # Here's the error:
  mutate(ClassDecrease = 
           case_when(
            LowClassSplit == Class_to_Count ~ (integer(trunc(ClassSubclass)) - PreSplitClass),
            TRUE ~ 0L
           )
         )
  
  print.data.frame(excelCopy)

CodePudding user response:

replacing integer with as.integer in your code gives what you expect.

case_when(
  excelCopy$LowClassSplit == Class_to_Count ~ (as.integer(trunc(excelCopy$ClassSubclass)) - excelCopy$PreSplitClass),
  TRUE ~ 0L)
[1]  0  0  0  0  0 -1
  • Related