Search code examples
rapplylapplysapplytapply

Print the 3rd number and all the elements divisible by 2


nums <- 1:20
result <- nums[fill the function]
result

Please anyone can tell me what function to use


Solution

  • You could do :

    sort(c(nums[nums %% 2 == 0], nums[3]))
    #[1]  2  3  4  6  8 10 12 14 16 18 20
    

    nums %% 2 == 0 is used to subset numbers which are divisible by 2 and we add nums[3] to get the 3rd number. We print the final output after sorting.