Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut function alternative in R

Tags:

regex

r

dplyr

I have some data in the form:

Person.ID    Household.ID    Composition 
   1             4593           1A_0C
   2             4992           2A_1C
   3             9843           1A_1C 
   4             8385           2A_2C  
   5             9823           8A_1C 
   6             3458           1C_9C 
   7             7485           2C_0C 
   :               :              :    

We can think of the composition variable as a count of adults/children i.e. 2A_1C would equate to two adults and two children.

What I want to do is reduce the amount of possible levels of composition. For person 5 we have composition of 8A_1C, I am looking for a way to reduce this to 4+A_0C. So for example we would have 4+ for any composition value with greater than 4A.

Person.ID     Household.ID     Composition 
    5             9823            4+A_1C
    6             3458             1A_4+C
    :               :                :

I am unsure of how to do this in R, I am thinking of using filter() or select() from dyplyr. Otherwise I would need to use some sort of regular expression.

Any help would be appreciated. Thanks

like image 792
williamg15 Avatar asked Jul 21 '26 08:07

williamg15


2 Answers

Data:

Person.ID <- c(1,2,3,4,5,6,7,8)
Household.ID <- c(4593,4992,9843,8385,9823,3458,7485)
Composition <- c("1A_0C","2A_1C","1A_1C","2A_2C","8A_1C","1A_9C","2A_0C")
dat <- tibble(Person.ID, Household.ID, Composition)

Function:

above4 <- function(f){
    ff <- gsub("[^0-9]","",f)
    if(ff>4){return("4+")}
    if(ff<=4){return(ff)}
}

Apply function (done on separated data, but can recombine after):

dat_ <- dat %>% tidyr::separate(., col=Composition, 
                           into=c("Adults", "Children"), 
                           sep="_") %>%
        dplyr::mutate(Adults_ = unlist(lapply(Adults,above4)), 
                         Children_ = unlist(lapply(Children,above4)))

You might then use select, filter to get your required dataset.

dat_ %>% dplyr::mutate(Composition_ = paste0(Adults_, "A_", Children_, "C")) %>%
         dplyr::select(Person.ID, Household.ID, Composition=Composition_)

 # A tibble: 7 x 3
      Person.ID Household.ID Composition
          <dbl>        <dbl> <chr>
    1        1.        4593. 1A_0C
    2        2.        4992. 2A_1C
    3        3.        9843. 1A_1C
    4        4.        8385. 2A_2C
    5        5.        9823. 4+A_1C
    6        6.        3458. 1A_4+C
    7        7.        7485. 2A_0C
like image 152
bruce.moran Avatar answered Jul 23 '26 23:07

bruce.moran


We can use gsub:

df$Composition <- gsub("(?<!\\d)([5-9]|\\d{2,})(?=[AC])", "4+", df$Composition, perl = TRUE)

This assumes that 2 or more consecutive digits represent a number that's always greater than 4 (i.e. no 01, 02, or 001).

Output:

  Person.ID Household.ID Composition
1         1         4593       1A_0C
2         2         4992       2A_1C
3         3         9843       1A_1C
4         4         8385       2A_2C
5         5         9823      4+A_1C
6         6         3458      1C_4+C
7         7         7485       2C_0C
like image 21
acylam Avatar answered Jul 23 '26 22:07

acylam