Running Apply with Function of prime number on data Frame

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Running Apply with Function of prime number on data Frame



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


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


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)


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))),]


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.





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 6 at 1:00



prime.function


x


sqrt(x)


x-1


prime.function


TURE


TRUE


data.frame


df[sapply(1:nrow(df), prime.function), ]




1 Answer
1



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), ]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard