in Education by
My initial dataframe looks: library(tidyverse) df <- tibble::tribble( ~element, ~label, ~value, "aa", "sessions", 196, "bb", "sessions", 865, "aa", "begin", 59, "bb", "begin", 123, "aa", "complete", 5, "bb", "complete", 5 ) I want to aggregate like, in a new dataframe: for each row will contain a column containing the ratio begin/sessions complete/sessions for each element aa and bb. Looking like: df_agg <- tibble::tribble( ~label_2, ~aa, ~bb, "begin_to_sessions", 0.301020408, 0.142196532, "complete_to_sessions", 0.005780347, 0.005780347 ) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It can be done with first spread it to 'wide' format, get the ratios, gather to 'long' format and spread it back to 'wide' format library(tidyverse) df %>% spread(label, value) %>% transmute(element, begin_to_sessions = begin/sessions, complete_to_sessions = complete/sessions) %>% gather(label_2, val, -element) %>% spread(element, val) Or using mutate_at (in case there are many columns) df %>% spread(label, value) %>% mutate_at(vars(begin, complete), list(~ ./sessions)) %>% select(-sessions) %>% rename_at(vars(begin, complete), ~ paste0(., "_to_sessions")) %>% gather(label_2, val, -element) %>% spread(element, val) # A tibble: 2 x 3 # label_2 aa bb # #1 begin_to_sessions 0.301 0.142 #2 complete_to_sessions 0.0255 0.00578 We can also avoid multiple gather/spread by doing a group_by division extracting the 'value' corresponding to 'sessions' string in 'label', filter out the rows having 'sessions' in 'label' and then do a single spread at the end df %>% group_by(element) %>% mutate(value = value/value[label == "sessions"]) %>% ungroup %>% filter(label != "sessions") %>% transmute(element, value, label2 = paste0(label, "_to_sessions")) %>% spread(element, value)

Related questions

0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    How can I sort a data.frame by multiple columns. E.x. I would like to sort the column q(descending) by column f (ascending) dd...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I use the function apply whenever I want to do something map py in R I want to know about the ... to replace these entirely? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    My data looks as follows: ID my_val db_val a X X a X X a Y X b X Y b Y Y b ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    How to replace the values of NA with zeros in some column and data frame I have? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I have the following data frame, I want to find the name of the column with the minimum date for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Can someone help with this: in a new df2 var2 should be added and df2$var1 should be expanded by df1$var2, filling new lines in Var1 & Var3 as given by variables in df1: (df1...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    _________ extract a subset of rows from a dataframe based on logical conditions. (a) rename (b) filter (c ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which of the following sort a dataframe by the order of the elements in B? (a) x[rev(order(x$B)), ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Can someone tell me a function which can return a specified no. of rows picked randomly without any replacement from a DataFrame in R? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to exclude specific participants from a DataFrame and create it from an existing DataFrame. I want a method ... worked for me. Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    There are two different methods for accessing the element of a list or data frame- [ ] and [[ ]] , ... the difference between them? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I have a dataframe as below itm Date Amount 67 420 2012-09-30 00:00:00 65211 68 421 2012-09-09 00 ... solutions would be appreciated. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
...