0

Basically the exact same question as this but in R. I have:

nums <- c("0:0:0:1", "0:0:1:0", "1:0:1:0")

I would like to know which ones have 1 more than once, so the answer would be FALSE FALSE TRUE

I tried:

library(stringr)
str_detect(nums, "1{2,}")
[1] FALSE FALSE FALSE

as well as lots of other regex guesses but cannot get it right.

Liam
  • 159
  • 11

4 Answers4

2

How about using str_count?:

library(stringr)
str_count(nums, "1")
[1] 1 1 2

If you want to subset on strings where 1occurs more than once:

nums[str_count(nums, "1") > 1]
[1] "1:0:1:0"

Data:

nums <- c("0:0:0:1", "0:0:1:0", "1:0:1:0")
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
1

Even though user2974951's solution is easier, this one might come in handy as well:

sapply(strsplit(nums,""), function(x) table(x)["1"] >= 2)
    1     1     1 
FALSE FALSE  TRUE 
Maël
  • 45,206
  • 3
  • 29
  • 67
1

1) If there are always the same number of fields, as in the example in the question, then read them in and sum the number that equal 1. If we knew the components were always 0 or 1, as in the example in the question, we could optionally omit the ==1.

rowSums(read.table(text = nums, sep = ":") == 1) > 1
## [1] FALSE FALSE TRUE

2) If 1 cannot appear as part as part of a longer string, which is the case in the example in the question, then

nchar(gsub("[^1]", "", nums)) > 1
## [1] FALSE FALSE TRUE

3) If there are not the same number of fields in each component and 1 can appear as part of a larger field then preprocess the strings to remove fields not equal to 1 and then apply the above.

library(gsubfn)
tmp <- gsubfn("[^:]+", ~ +(x == 1), nums)
nchar(gsub("[^1]", "", tmp)) > 1
## [1] FALSE FALSE TRUE
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1

You can try

> lengths(regmatches(nums, gregexpr("1", nums))) > 1
[1] FALSE FALSE  TRUE
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81