Is there a more concise method to filter columns containing a number greater than or less than, in dplyr?

reubenmcg

Here is a sample of my tibble

    protein patient           value 
  <chr>   <chr>             <dbl> 
1 BOD1L2  RF0064_Case-9-d-   10.4  
2 PPFIA2  RF0064_Case-20-d-  7.83 
3 STAT4   RF0064_Case-11-d-  11.0 
4 TOM1L2  RF0064_Case-29-d-  13.0  
5 SH2D2A  RF0064_Case-2-d-   8.28  
6 TIGD4   RF0064_Case-49-d-  9.71  

In the "patient" column the "d" as in "Case-x-d" represents the a number of days. What I would like to do is create a new column stating whether the strings in the "patient" column contain values less than 14d.

I have managed to do this using the following command:

under14 <- "-1d|-2d|-3d|-4d|-4d|-5d|-6d|-7d|-8d|-9d|-11d|-12d|-13d|-14d"

data <- data %>%
    mutate(case=ifelse(grepl(under14,data$patient),'under14days','over14days'))

However this seems extremely clunky and actually took way to long to type. I will have to be changing my search term many times so would like a quicker way to do this? Perhaps using some kind of regex is the best option, but I don't really know where to start with this.

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/en_NZ.UTF-8/C/en_NZ.UTF-8/en_NZ.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] readxl_1.1.0    Rmisc_1.5       plyr_1.8.4      lattice_0.20-35 forcats_0.3.0   stringr_1.3.1   dplyr_0.7.5     purrr_0.2.5    
 [9] readr_1.1.1     tidyr_0.8.1     tibble_1.4.2    ggplot2_2.2.1   tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17     cellranger_1.1.0 pillar_1.2.3     compiler_3.5.0   bindr_0.1.1      tools_3.5.0      lubridate_1.7.4 
 [8] jsonlite_1.5     nlme_3.1-137     gtable_0.2.0     pkgconfig_2.0.1  rlang_0.2.1      psych_1.8.4      cli_1.0.0       
[15] rstudioapi_0.7   yaml_2.1.19      parallel_3.5.0   haven_1.1.1      bindrcpp_0.2.2   xml2_1.2.0       httr_1.3.1      
[22] hms_0.4.2        grid_3.5.0       tidyselect_0.2.4 glue_1.2.0       R6_2.2.2         foreign_0.8-70   modelr_0.1.2    
[29] reshape2_1.4.3   magrittr_1.5     scales_0.5.0     rvest_0.3.2      assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2
[36] utf8_1.1.4       stringi_1.2.3    lazyeval_0.2.1   munsell_0.5.0    broom_0.4.4      crayon_1.3.4    
> 
Maurits Evers

One possibility is to use tidyr::separate

library(tidyverse)
df %>%
    separate(patient, into = c("ID1", "Days", "ID2"), sep = "-", extra = "merge", remove = F) %>%
    mutate(case = ifelse(as.numeric(Days) <= 14, "under14days", "over14days")) %>%
    select(-ID1, -ID2)
#  protein           patient Days value        case
#1  BOD1L2  RF0064_Case-9-d-    9 10.40 under14days
#2  PPFIA2 RF0064_Case-20-d-   20  7.83  over14days
#3   STAT4 RF0064_Case-11-d-   11 11.00 under14days
#4  TOM1L2 RF0064_Case-29-d-   29 13.00  over14days
#5  SH2D2A  RF0064_Case-2-d-    2  8.28 under14days
#6   TIGD4 RF0064_Case-49-d-   49  9.71  over14days

Sample data

df <-read.table(text =
    "    protein patient           value
1 BOD1L2  RF0064_Case-9-d-   10.4
2 PPFIA2  RF0064_Case-20-d-  7.83
3 STAT4   RF0064_Case-11-d-  11.0
4 TOM1L2  RF0064_Case-29-d-  13.0
5 SH2D2A  RF0064_Case-2-d-   8.28
6 TIGD4   RF0064_Case-49-d-  9.71  ", header = T, row.names = 1)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

dplyr left_join by less than, greater than condition

分類Dev

Create new column that lists the names of columns containing values greater than a certain number

分類Dev

Unique sums greater than a number

分類Dev

Regex in Java for "greater than" or "less than" without using bracket "<|>" symbols

分類Dev

Greater Than and Less Than Symbols in C# Regular Expression

分類Dev

Product of prime factors of a number, less than that number

分類Dev

Is there a more concise way to initialize a unique_ptr<char[]> than this?

分類Dev

Is `tail -f` more efficient than `less +F`?

分類Dev

Grid column responsive width but no more and no less than

分類Dev

xpath - search for number greater than and show parents

分類Dev

sort with -k greater than number of keys

分類Dev

jQuery - test if img width is 'more than' and 'less than'

分類Dev

Why less than (<) has much better performance than greater than (>) in a query?

分類Dev

what is the most efficient way of checking the greater than and less then in Java?

分類Dev

Split string into string list containing strings no greater than n chars

分類Dev

VBA - filter more than one AutoCAD block

分類Dev

Haskell filter more than one char

分類Dev

LDAP More than one negation operators in filter

分類Dev

Invalid version number: Version number may be negative or greater than 255

分類Dev

Regular Expression for matching number greater than specified decimal number

分類Dev

What is the right way of using "greater than", "less than" comparison on nullable integers in Kotlin?

分類Dev

Why does escaping greater than or less than work in bash string comparisons?

分類Dev

Does anyone know the code for the symbol less than or greater than \lessgtr in latex

分類Dev

pandas remove rows when consecutive row values of particular columns are more than a number

分類Dev

PHP number more than 10 decimal places

分類Dev

filter orderby is not working as expected for values greater than 10 in angular 4

分類Dev

How to filter rows in Excel 2016 that have count greater than 1?

分類Dev

FakeItEasy ReturnLazily with more than 4 arguments method

分類Dev

How to plot columns numbered greater than 1000 in gnuplot

Related 関連記事

  1. 1

    dplyr left_join by less than, greater than condition

  2. 2

    Create new column that lists the names of columns containing values greater than a certain number

  3. 3

    Unique sums greater than a number

  4. 4

    Regex in Java for "greater than" or "less than" without using bracket "<|>" symbols

  5. 5

    Greater Than and Less Than Symbols in C# Regular Expression

  6. 6

    Product of prime factors of a number, less than that number

  7. 7

    Is there a more concise way to initialize a unique_ptr<char[]> than this?

  8. 8

    Is `tail -f` more efficient than `less +F`?

  9. 9

    Grid column responsive width but no more and no less than

  10. 10

    xpath - search for number greater than and show parents

  11. 11

    sort with -k greater than number of keys

  12. 12

    jQuery - test if img width is 'more than' and 'less than'

  13. 13

    Why less than (<) has much better performance than greater than (>) in a query?

  14. 14

    what is the most efficient way of checking the greater than and less then in Java?

  15. 15

    Split string into string list containing strings no greater than n chars

  16. 16

    VBA - filter more than one AutoCAD block

  17. 17

    Haskell filter more than one char

  18. 18

    LDAP More than one negation operators in filter

  19. 19

    Invalid version number: Version number may be negative or greater than 255

  20. 20

    Regular Expression for matching number greater than specified decimal number

  21. 21

    What is the right way of using "greater than", "less than" comparison on nullable integers in Kotlin?

  22. 22

    Why does escaping greater than or less than work in bash string comparisons?

  23. 23

    Does anyone know the code for the symbol less than or greater than \lessgtr in latex

  24. 24

    pandas remove rows when consecutive row values of particular columns are more than a number

  25. 25

    PHP number more than 10 decimal places

  26. 26

    filter orderby is not working as expected for values greater than 10 in angular 4

  27. 27

    How to filter rows in Excel 2016 that have count greater than 1?

  28. 28

    FakeItEasy ReturnLazily with more than 4 arguments method

  29. 29

    How to plot columns numbered greater than 1000 in gnuplot

ホットタグ

アーカイブ