Site icon R-bloggers

How to put margins on tables or arrays in R?

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to put margins on tables or arrays in R? appeared first on Data Science Tutorials

What do you have to lose?. Check out Data Science tutorials here Data Science Tutorials.

How to put margins on tables or arrays in R?, I’ll show you how to use the addmargins function in the R programming language to add margins to tables or arrays in this post.

Algorithm Classifications in Machine Learning – Data Science Tutorials

The addmargins function will be used in the tutorial’s two examples to annotate the margin values on table objects.

To be more specific, the instruction includes the following details:

Providing Examples of Data

The data listed below will serve as the foundation for this R tutorial.

data <- data.frame(x1 = c(LETTERS[1:3], "B", "B", "C"),x2 = letters[1:2])
data                                         
   x1 x2
1  A  a
2  B  b
3  C  a
4  B  b
5  B  a
6  C  b

Consider the previous table. It demonstrates how the sample data frame has two variables and six rows.

Boosting in Machine Learning:-A Brief Overview (datasciencetut.com)

Then, using these data, we can build a table object (a contingency table).

tab <- table(data)                        
tab
    x2
x1  a b
  A 1 0
  B 1 2
  C 1 1

Example 1:-Incorporate Sum Margins into the Contingency Table.

Addmargins() Function Utilization

I’ll show you how to add the sum of each row and column to a table’s margins using the addmargins function in Example 1.

Add Significance Level and Stars to Plot in R (datasciencetut.com)

Take into account the R code below:

tab_sum <- addmargins(tab, FUN = sum)   
tab_sum                                       
         x2
x1    a b sum
  A   1 0   1
  B   1 2   3
  C   1 1   2
  sum 3 3   6

As you can see, we have a row and column that contain the sum margins of our data that are annotated.

 Hope you enjoyed it.

Check your inbox or spam folder to confirm your subscription.

What is the best way to filter by row number in R? – Data Science Tutorials

The post How to put margins on tables or arrays in R? appeared first on Data Science Tutorials

Learn how to expert in the Data Science field with Data Science Tutorials.

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.