Im trying to store all the prime numbers in a vector into an object.
Q8.16 <-function(vector){ for(i in 1:length(vector)){ if (vector[i] == 2L || all(vector[i] %% 2L:ceiling(sqrt(vector[i])) != 0)){ prime1<-NULL prime1<-append(prime1,vector[i]) } } prime1 } sample1<-sample(1:1000 ,1000) Q8.16(sample1) The output I get from it is only one digit instead of a vector with all the prime numbers. Why is this happening?
emptyvectoris never used, andreturnexits the function immediately when it finds the first prime number.