0

I am looking up to index data frame by applying prime Number function on rows of data frame using apply. the given output should be holding out rows which have any or all numbers as prime. Example data frame name c4:

c1 c2 c3 1 8 2 6 2 9 5 4 3 10 4 5 4 7 1 8 5 3 1 2 6 7 5 9 7 5 1 4 8 2 1 3 9 7 2 4 10 10 4 8

the given output should be

c1 c2 c3 1 8 2 6 2 9 5 4 3 10 4 5 4 7 1 8 5 3 1 2 6 7 5 9 7 5 1 4 8 2 1 3 9 7 2 4

removing the tenth row. the code I am using for the prime number is

prime.function<- function (x){ if(x == 2){ return(TURE) } else if (any(x %% 2: (x - 1) == 0)){ return(FALSE) }else{ return(TRUE) } }

and I am using the following function in apply function in base indexing :

c4[apply(c4, MARGIN = 1, FUN = function(x) any(prime.function(x))),]

but unfortunately, the code throws error.

I would love to hear any suggestion about the code. Hope I am clear on the explanation side.

Mohammed
  • 11
  • 5
  • 2
    First off, `prime.function` is a *very* inefficient way to check whether `x` is prime. For example and amongst other things, it suffices to check for numbers up to `sqrt(x)` rather than `x-1`. Secondly you state that *"the code throws error"*; what errors are produced? For example, at the moment `prime.function` has a typo (`TURE` instead of `TRUE`); are there other errors? It seems to me you want to check whether a row number is prime or not. If that is the case, why loop through the entire `data.frame`? Unless I misunderstood, `df[sapply(1:nrow(df), prime.function), ]` should be enough. – Maurits Evers Aug 06 '18 at 01:00

1 Answers1

0

Using the prim function provided here: Prime number function in R

is.prime <- function(n) n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0)

The following code will do the job:

# replicate your data frame
c4_check <- c4

# replace the items with booleans in the replicate
c4_check[] <- sapply(unlist(c4), is.prime)

# filter the original data frame using the replicate
c4[rowSums(c4_check) != 0, ]

Update:

@Maurits Evers' answer in his comment is much better:

c4[sapply(1:nrow(c4), is.prime), ]
OzanStats
  • 2,756
  • 1
  • 13
  • 26