How to sum a column by group in R

How to sum a column by group in R

In this tutorial, we will learn how to compute the sum of a column by groups in another column in a dataframe. Basic, idea is to group the dataframe by the grouping variable/column and then find the sum for each group. Let us get started by loading tidyverse suite of R packages. We will be… Continue reading How to sum a column by group in R

How to extract a column of a dataframe as a vector in R

Extract A Column As Vector in R

In this tutorial, we will learn how to extract a column from a dataframe in R. We will extract a column as a vector using base R method and then see multiple examples of using dplyr’s pull() function to extract a column from a dataframe. Getting Ready with Packages and Data Let us get started… Continue reading How to extract a column of a dataframe as a vector in R

How to number rows within a group in dataframe

Add Numbers to rows within groups in dataframe

In this tutorial, how to add numbers to rows with in each group in a dataframe in R. The picture below illustrates an example of adding row numbers per group. Packages And Data We will first create sample dataframe using tibble function and then use dplyr’s functions to add sequence of numbers to each group… Continue reading How to number rows within a group in dataframe

dplyr count(): count unique values of a variable

In this tutorial, we will learn how to count unique values of a variable(s) or column using dplyr’s count() function. dplyr’s count() function is a convenient function that combines grouping operation and counting the number of observation in each group. We will learn of 4 examples of using dplyr’s count() function with different arguments. First,… Continue reading dplyr count(): count unique values of a variable

How to randomly select rows from a dataframe in R

In this tutorial, we will learn how to randomly select rows from a dataframe using dplyr’s slice_sample() function in R. slice_sample() is the new way to randomly select rows either with replacement or without replacement and it supersedes an earlier function sample_n() in dplyr. We will use storms dataset available with ggplot2 R package to… Continue reading How to randomly select rows from a dataframe in R

Exit mobile version