mardi 26 novembre 2019

Mutate with ifelse in r

I'm working on a data frame (dim: 10,155 by 33). Few rows & columns of the data frame are

  rg[1:3, 1:4]

  REF_NO children age_band  status
1   2148        1    45-50 Partner
2   8099        1    61-65 Partner
3   6611        3    31-35 Partner


> table(rg_age_band)

  18-21   22-25   26-30   31-35   36-40   41-45   45-50   51-55 
     63     456     927    1061    1134    1112    1359    1052 
  55-60   61-65   65-70     71+ Unknown 
   1047     881     598     410      55 

For variable, age_band, I want to use tidyverse functions separate(), mutate() & chaining operator for following nested operations:

  1. separate age_band into two columns a1 & a2
  2. replace '71+' in a1 column with '71'
  3. Convert columns a1 and a2 to numeric class
  4. create a column 'age' which is the avergae of a1 & a2 columnss
  5. drop columns a1 and a2

I'm using the following code:

library(tidyr); library(dplyr)

rg1=rg %>% 
  separate(age_band, into = c("a1", "a2"), sep="-") %>% 
  mutate(a1 = as.numeric(ifelse(rg$a1=="71+", 71, rg$a1)),
         a2 = as.numeric(a2),
         age = 0.5*(a1+a2)) %>% 
  select(-a1-a2)

Error: Column `a1` must be length 10155 (the number of rows) or one, not 0 

Error: Column a1 must be length 10155 (the number of rows) or one, not 0 Please suggest what can be done. And when I run the code without '$' in ifelse statement, I get an error object 'a1' not found while usually, we don't need '$' while using chaining operator & mutate. Discussion on [Similar question][1] is not able to give any useful solution. I tried the peices of code and the problem is with

mutate(a1 = as.numeric(ifelse(rg$a1=="71+", 71, rg$a1))

also

#is producing warning 
Expected 2 pieces. Missing pieces filled with `NA` in 465 rows```


  [1]: https://stackoverflow.com/questions/55662552/r-mutate-ifelse-update-conditional-row-with-calculated-function-value

Aucun commentaire:

Enregistrer un commentaire