Hadley Wickham and Twitter

A few days ago, Hadley Wickham tweeted at our RGalleon account in regards to an example I provided in the video.  The question asked the user to count the number of p values less than .05 from multiple t tests performed.  We provide the image of the conversation down below.

Twitter @hadleywickham

I am honored that he would watch my video and make such a great suggestion, as his does require less code and is more computationally efficient.  For those that are more familiar with R, you should definetly use his solution instead of the one I provided.  Mine was the following

# If-Else Example

load('For Loops data 1.RData') #load data into R
ls() #display objects in R

pvalues<-c() # make vector to hold the p values
finalpeas<-c() # make vecotr to hold 1's and 0's

for (i in 1:100){ #starts the for loop

	pvalues[i]<-t.test(Matrix[,i], Vector)$p.value # performs that test 100 times, notice that we only use the index for the matrix (and only for the column) and the vector pvalues
	
	if(pvalues[i]<.05){
	
		finalpeas[i]<-1

	}
	else{
	
		finalpeas[i]<-0
	
	}
}

sum(finalpeas) #shows the amount of p values less than .05

I should note however, that if you are still learning R and how to use If-Else loops, my solution does help you practice how to use them.  It is also important to recognize that there may be multiple different solutions to a coding problem.  More often then not, a solution that requires less lines of code is better.

 

If you would like to see the video, click here.

Leave a Reply