Contingency tables are useful short hand ways of inputting and visualizing data. I have yet to find an easy way to convert between contingency tables and data frames in R. Below is a short script in which I input a contingency table, create a function to convert the 2 by 2 table to a data frame, and convert the data frame back to a table. These operations are useful for running some statistical operations that either only work on tables or only work on data frames. Hope the below example is useful.
A repository of programs, scripts, and tips essential to
genetic epidemiology, statistical genetics, and bioinformatics
Welcome to the Genome Toolbox! I am glad you navigated to the blog and hope you find the contents useful and insightful for your genomic needs. If you find any of the entries particularly helpful, be sure to click the +1 button on the bottom of the post and share with your colleagues. Your input is encouraged, so if you have comments or are aware of more efficient tools not included in a post, I would love to hear from you. Enjoy your time browsing through the Toolbox.
Showing posts with label dataframe. Show all posts
Showing posts with label dataframe. Show all posts
Friday, November 21, 2014
Tuesday, April 1, 2014
Create Empty Data Frame in R with Specified Dimensions
Sometimes it is necessary to create an empty data frame in R to fill with output. An example would be output from a for loop that loops over SNPs and calculates an association p-value to fill into the empty data frame. The matrix functionality in R makes this easy to do. The code below will create a 100 by 2 matrix and convert it into a dataframe with 100 rows and 2 column variables. The nrow and ncol options are what designate the size of the resulting data frame. The data frame is filled with NA values so that missing values are handled appropriately. Below is some example code:
Subscribe to:
Posts (Atom)