Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge rows in tibble

I'd like to list all Functions my package in a table.

So far I extracted all functions and title from the packages help docs

library(magrittr)
package_info <- library(help = magrittr)$info[[2]]
package_info_tbl <- package_info %>% 
  stringr::str_split(pattern = "\\s+", n = 2, simplify = T) %>%
  tibble::as_tibble(.name_repair = "minimal")
colnames(package_info_tbl) <- c("Function", "Title")

package_info_tbl
#> # A tibble: 13 x 2
#>    Function     Title                                          
#>    <chr>        <chr>                                          
#>  1 "%$%"        magrittr exposition pipe-operator              
#>  2 "%<>%"       magrittr compound assignment pipe-operator     
#>  3 "%>%"        magrittr forward-pipe operator                 
#>  4 "%T>%"       magrittr tee operator                          
#>  5 "[[.fseq"    Extract function(s) from a functional sequence.
#>  6 "debug_fseq" Debugging function for functional sequences.   
#>  7 "debug_pipe" Debugging function for magrittr pipelines.     
#>  8 "extract"    Aliases                                        
#>  9 "freduce"    Apply a list of functions sequentially         
#> 10 "functions"  Extract the function list from a functional    
#> 11 ""           sequence.                                      
#> 12 "magrittr"   magrittr - Ceci n'est pas un pipe              
#> 13 "print.fseq" Print method for functional sequence.

Created on 2020-03-29 by the reprex package (v0.3.0)

I found that some rows are seperated and result in 2 or more rows if the title is long. How to merge these rows?

like image 809
Thomas Avatar asked Mar 29 '20 14:03

Thomas


People also ask

How do I append to a Dataframe in Tibble?

Data frame to append to. ... < dynamic-dots > Name-value pairs, passed on to tibble (). Values can be defined only for columns that already exist in .data and unset columns will get an NA value. One-based row index where to add the new rows, default: after last row.

How do I create a Tibble in R?

The tibble R package provides easy to use functions for creating tibbles, which is a modern rethinking of data frames. # Installing install.packages ("tibble") # Loading library ("tibble") To create a new tibble from combining multiple vectors, use the function data_frame ():

How do I create a Tibble from combining multiple vectors?

To create a new tibble from combining multiple vectors, use the function data_frame (): # Create friends_data <- data_frame ( name = c ("Nicolas", "Thierry", "Bernard", "Jerome"), age = c (27, 25, 29, 26), height = c (180, 170, 185, 169), married = c (TRUE, FALSE, TRUE, TRUE) ) # Print friends_data

What is the advantage of using a Tibble over a spreadsheet?

Tibbles have nice printing method that show only the first 10 rows and all the columns that fit on the screen. This is useful when you work with large data sets.


2 Answers

We can replace blank with NA values, use fill to replace NA with previous value in Function column, group_by Function and create one concatenated string for each Function.

library(dplyr)

package_info_tbl %>%
  na_if('') %>%
  tidyr::fill(Function)  %>%
  group_by(Function) %>%
  summarise(Title = paste(Title, collapse = " "))


# A tibble: 12 x 2
#   Function   Title                                                
#   <chr>      <chr>                                                
# 1 [[.fseq    Extract function(s) from a functional sequence.      
# 2 %<>%       magrittr compound assignment pipe-operator           
# 3 %>%        magrittr forward-pipe operator                       
# 4 %$%        magrittr exposition pipe-operator                    
# 5 %T>%       magrittr tee operator                                
# 6 debug_fseq Debugging function for functional sequences.         
# 7 debug_pipe Debugging function for magrittr pipelines.           
# 8 extract    Aliases                                              
# 9 freduce    Apply a list of functions sequentially               
#10 functions  Extract the function list from a functional sequence.
#11 magrittr   magrittr - Ceci n'est pas un pipe                    
#12 print.fseq Print method for functional sequence.               
like image 53
Ronak Shah Avatar answered Oct 24 '22 18:10

Ronak Shah


If blank, fill Function columns with the value from previus row. And collapse Title if Function is same.

package_info_tbl$Function <- Reduce(function(x,y) if (y=="") x else y, package_info_tbl$Function, acc=T) %>%

package_info_tbl <- package_info_tbl %>% 
  group_by(Function) %>%
  summarise(Title = paste(Title, collapse  = " "))

Or, combined into your dplyr chain

package_info_tbl <- package_info %>% 
      stringr::str_split(pattern = "\\s+", n = 2, simplify = T) %>%
      tibble::as_tibble(.name_repair = "minimal") %>%
      setNames(., c("Function", "Title")) %>%
      mutate(Function = Reduce(function(x,y) if (y=="") x else y, Function, acc=T)) %>%
      group_by(Function) %>%
      summarise(Title = paste(Title, collapse  = " ")) %>%
      ungroup

Output

package_info_tbl

# # A tibble: 12 x 2
#    Function   Title                                                
#    <chr>      <chr>                                                
#  1 %$%        magrittr exposition pipe-operator                    
#  2 %<>%       magrittr compound assignment pipe-operator           
#  3 %>%        magrittr forward-pipe operator                       
#  4 %T>%       magrittr tee operator                                
#  5 [[.fseq    Extract function(s) from a functional sequence.      
#  6 debug_fseq Debugging function for functional sequences.         
#  7 debug_pipe Debugging function for magrittr pipelines.           
#  8 extract    Aliases                                              
#  9 freduce    Apply a list of functions sequentially               
# 10 functions  Extract the function list from a functional sequence.
# 11 magrittr   magrittr - Ceci n'est pas un pipe                    
# 12 print.fseq Print method for functional sequence.  
like image 20
nurandi Avatar answered Oct 24 '22 20:10

nurandi