How to extract one specific group in dplyr - r
Given a grouped tbl, can I extract one/few groups?
Such function can be useful when prototyping code, e.g.:
mtcars %>%
group_by(cyl) %>%
select_first_n_groups(2) %>%
do({'complicated expression'})
Surely, one can do an explicit filter before grouping, but that can be cumbersome.
With a bit of dplyr along with some nesting/unnesting (supported by tidyr package), you could establish a small helper to get the first (or any) group
first = function(x) x %>% nest %>% ungroup %>% slice(1) %>% unnest(data)
mtcars %>% group_by(cyl) %>% first()
By adjusting the slicing you could also extract the nth or any range of groups by index, but typically the first or the last is what most users want.
The name is inspired by functional APIs which all call it first (see stdlibs of i.e. kotlin, python, scala, java, spark).
Edit: Faster Version
A more scalable version (>50x faster on large datasets) that avoids nesting would be
first_group = function(x) x %>%
select(group_cols()) %>%
distinct %>%
ungroup %>%
slice(1) %>%
{ semi_join(x, .)}
A another positive side-effect of this improved version is that it fails if not grouping is present in x.
Try this where groups is a vector of group numbers. Here 1:2 means the first two groups:
select_groups <- function(data, groups, ...)
data[sort(unlist(attr(data, "indices")[ groups ])) + 1, ]
mtcars %>% group_by(cyl) %>% select_groups(1:2)
The selected rows appear in the original order. If you prefer that the rows appear in the order that the groups are specified (e.g. in the above eaxmple the rows of the first group followed by the rows of the second group) then remove the sort.
Related
Tidymodels infer chisq_test count column
I am using the infer library to run a chisq_test in a group_by with subgroup ~ answer. I have, among others, a column with subgroup, one with answers and one with count. Is it possible to specify the count column when running dat <- dat %>% group_by(Question, Group) %>% mutate(p_value = chisq_test(cur_data(), Subgroup ~ Answer)$p_value) %>% ungroup() Or do I need to use uncount(Count) first?
Filtering by Numerical Variable but Need to Satisfy Multiple Categorical Groups
I'm working with a modified version of the babynames dataset, which can be gotten by installing the babynames packages and calling: # to install the package install.packages('babynames') # to load the package library(babynames) # to get the only one dataframe of interest from the package babynames <- babynames::babynames # the modified data that I'm working with babynames_prop_sexes <- babynames %>% select(-prop, -year) %>% group_by(name, sex) %>% mutate(total_occurence = sum(n)) I need to sort out names that have more than 10000 occurrences for both sexes. How can I approach this? (Preferably by using dplyr but any method is welcomed.) Thanks in advance for any help!
There might be a more elegant solution. But this should get you a list of names that appear with > 10000 entries as both an M and an F. For the method, I just kept going with dplyr verbs. After using filter to get rid of the entries that appear < 10000 times, I could then group_by the name and use tally(), knowing that n = 2 when that entry appeared twice, once for M and once for F. large_total_both_genders_same_name <- babynames %>% group_by(name, sex) %>% summarize(total = sum(n)) %>% filter(total > 10000) %>% arrange(name) %>% group_by(name) %>% tally() %>% arrange(desc(n)) %>% filter(n == 2) %>% dplyr::select(name) And if you want to filter your original file by that shortlist of names you can use a semi_join on the table we created, to shorten up the list. In this case, it wouldn't be obvious what you are looking at unless you also included the year column, which you removed. original_babynames_shortened <- babynames_prop_sexes %>% filter(name %in% large_total_both_genders_same_name$name) But anyway, this is a common process. Create a summary table of some kind that is saved as its own 'intermediary' table, so to speak, then join that to your original, as a filter. Sometimes this can all be done in one go, but it's often easier, in my opinion to break this into two pieces.
Creating Groups based on Column Position
Good afternoon! I think this is pretty straight forward question, but I think I am missing a couple of steps. Would like to create groups based on column position. Am working with a dataframe / tibble; 33 rows long, and 66 columns wide. However, every sequence of 6 columns, should really be separated into its own sub-dataframe / tibble. The sequence of the number columns is arbitrary to the dataframe. Below is an attempt with mtcars, where I am trying to group every 2 columns into its own sub-dataframe. mtcars %>% tibble() %>% group_by(across(seq(1,2, length.out = 11))) %>% nest() However, that method generates errors. Something similar applies when working just within nest() as well. Using mtcars, would like to create groups using a sequence for every 3 columns, or some other number. Would ultimately like the mtcars dataframe to be... Columns 1:3 to be group 1, Columns 4:6 to be group 2, Columns 7:9 to be group 3, etc... while retaining the information for the rows in each column. Also considered something with pivot_longer... mtcars %>% tibble() %>% pivot_longer(cols = seq(1,3, by = 1)) ...but that did not generate defined groups, or continue the sequencing along all columns of the dataframe. Hope one of you can help me with this! Would make certain tasks for work much easier. PS - A plus if you can keep the workflow to tidyverse centric code :)
You could try this. It splits the dataframe into a list of dataframes based on the number of columns you want (3 in your example): library(tidyverse) list_of_dataframes <- mtcars %>% tibble() %>% mutate(row = row_number()) %>% pivot_longer(-row) %>% group_by(row) %>% mutate(group = ceiling(row_number()/ 3)) %>% ungroup() %>% group_split(group) %>% map( ~select(.x, row, name, value) %>% pivot_wider() ) EDIT Here, based on comments from the question asker, we will avoid pivoting the data. Instead, we map the groups across the dataframe. list_of_dataframes <- map(seq(1, ncol(mtcars), by = 3), ~mtcars %>% as_tibble() %>% select(all_of(.x:min(c(.x+2, ncol(mtcars)))))) We can then wrap this in a function to make it a little easier to use and change group sizes and dataframes: group_split_cols <- function(.data, ncols_per_group){ map(seq(1, ncol(.data), by = ncols_per_group), ~.data %>% as_tibble() %>% select(all_of(.x:min(c(.x+ncols_per_group-1, ncol(.data)))))) } list_of_dataframes <- group_split_cols(.data = mtcars, ncols_per_group = 3)
Using the R syntax sequence operator ":" within the the sum command with more then 50 columns
i would like to index by column name within the sum command using the sequence operator. library(dbplyr) library(tidyverse) df=data.frame( X=c("A","B","C"), X.1=c(1,2,3),X.2=c(1,2,3),X.3=c(1,2,3),X.4=c(1,2,3),X.5=c(1,2,3),X.6=c(1,2,3),X.7=c(1,2,3),X.8=c(1,2,3),X.9=c(1,2,3),X.10=c(1,2,3), X.11=c(1,2,3),X.12=c(1,2,3),X.13=c(1,2,3),X.14=c(1,2,3),X.15=c(1,2,3),X.16=c(1,2,3),X.17=c(1,2,3),X.18=c(1,2,3),X.19=c(1,2,3),X.20=c(1,2,3), X.21=c(1,2,3),X.22=c(1,2,3),X.23=c(1,2,3),X.24=c(1,2,3),X.25=c(1,2,3),X.26=c(1,2,3),X.27=c(1,2,3),X.28=c(1,2,3),X.29=c(1,2,3),X.30=c(1,2,3), X.31=c(1,2,3),X.32=c(1,2,3),X.33=c(1,2,3),X.34=c(1,2,3),X.35=c(1,2,3),X.36=c(1,2,3),X.37=c(1,2,3),X.38=c(1,2,3),X.39=c(1,2,3),X.40=c(1,2,3), X.41=c(1,2,3),X.42=c(1,2,3),X.43=c(1,2,3),X.44=c(1,2,3),X.45=c(1,2,3),X.46=c(1,2,3),X.47=c(1,2,3),X.48=c(1,2,3),X.49=c(1,2,3),X.50=c(1,2,3), X.51=c(1,2,3),X.52=c(1,2,3),X.53=c(1,2,3),X.54=c(1,2,3),X.55=c(1,2,3),X.56=c(1,2,3)) Is there a quicker way todo this. The following provides the correct result. However, for large datasets (larger than this one ) it becomes vary laborious to deal with especially when pivot_wider is used and the columns are not created before hand (like above) df %>% rowwise() %>% mutate( Result_column=case_when( X=="A"~ sum(c(X.1,X.2,X.3,X.4,X.5)), X=="B"~ sum(c(X.4,X.5)), X=="C" ~ sum(c( X.3, X.4, X.5, X.6, X.7, X.8, X.9, X.10, X.11, X.12, X.13, X.14, X.15, X.16, X.17, X.18, X.19, X.20, X.21, X.22, X.23, X.24, X.25, X.26, X.27, X.28, X.29, X.30, X.31, X.32, X.33, X.34, X.35, X.36, X.37, X.38, X.39, X.40, X.41, X.42,X.43, X.44, X.45, X.46, X.47, X.48, X.49, X.50, X.51, X.52, X.53, X.54, X.55, X.56)))) %>% dplyr::select(Result_column) The following is the how it would be used when using "select" syntax, which is that i would like to use. However, does not provide correct numerical solution. One can shorter the code by ~50 entries, by using a sequence operator ":". df %>% rowwise() %>% mutate( Result_column=case_when( X=="A"~ sum(c(X.1:X.5)), X=="B"~ sum(c(X.4:X.5)), X=="C" ~ sum(c(X.3:X.56)))) %>% dplyr::select(Result_column) below is a related question, however, not the same because what is needed is not a column that starts with "X" but rather a sequence. Using mutate rowwise over a subset of columns EDIT: the provided code (below) from cnbrowlie is correct. df %>% mutate( Result_column=case_when( X=="A"~ sum(c(X.1:X.5)), X=="B"~ sum(c(X.4:X.5)), X=="C" ~ sum(c(X.3:X.56)))) %>% dplyr::select(Result_column)
This can be done with dplyr>=1.0.0 using rowSums() (which computes the sum for a row across multiple columns) and across() (which superceded vars() as a method for specifying columns in a dataframe, allowing the use of : to select sequences of columns): df %>% rowwise() %>% mutate( Result_column=case_when( X=="A"~ rowSums(across(X.1:X.5)), X=="B"~ rowSums(across(X.4:X.5)), X=="C" ~ rowSums(across(X.3:X.56)) ) ) %>% dplyr::select(Result_column)
What does n=n( ) mean in R?
The other day I was reading the following lines in R and I don't understand what the %>% and summarise(n=n()) and summarise(total=n()) meant. I understand the group_by and ungroup methods though. Can someone help out? There isn't any documentation for this either. library(dplyr) net.multiplicity <- group_by(net, nodeid, epoch) %>% summarise(n=n()) %>% ungroup() %>% group_by(n) %>% summarise(total=n())
This is from the dplyr package. n=n() means that a variable named n will be assigned the number of rows (think number of observations) in the summarized data. the %>% is read as "and then" and is way of listing your functions sequentially rather then nesting them. So that command is saying you should do the grouping and then summarize the result of the grouping by the number of rows in each group and then ungroup that result, and then group the un-grouped data based on n and then summarize that by the total number of rows in each of the new groups.