Member-only story
With the development of dplyr or its umbrella package tidyverse, it becomes quite straightforward to perform operations over columns or rows in R. These column- or row-wise methods can also be directly integrated with other dplyr verbs like select
, mutate
, filter
and summarise
, making them more comparable with other functions in apply
or map
families. In this blog, I will briefly cover some useful manipulations over rows or columns.
1. Column-wise operation
Example 1: select those string columns with less than 5 levels in the dataset of starwars.
# solution 1: anonymous function (recommended)
starwars %>%
select_if(~ is.character(.x) & length(unique(.x)) <= 5)
We can use a convenient function select_if
to identify certain columns by multiple criteria. This is essentially equivalent to the following expanded format.
# solution 2: expanded form of function
starwars %>%
select_if(function(x) is.character(x) & length(unique(x)) <= 5)
It is worth noting that we are using tilde (~) to define an anonymous function, and thus we should use dot x (.x) to…