The previous answer to similar question had issues with incorrect cor.test output and had to fix the same (code below). However, I used while loop to extract all the cor.test