purrr | A functional programming toolkit for R | Functional Programming library
kandi X-RAY | purrr Summary
kandi X-RAY | purrr Summary
purrr enhances R’s functional programming (FP) toolkit by providing a complete and consistent set of tools for working with functions and vectors. If you’ve never heard of FP before, the best place to start is the family of map() functions which allow you to replace many for loops with code that is both more succinct and easier to read. The best place to learn about the map() functions is the iteration chapter in R for data science.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of purrr
purrr Key Features
purrr Examples and Code Snippets
Community Discussions
Trending Discussions on purrr
QUESTION
is there a simple way to unlist a list and keep the names of one level as a new variable?
Working example:
...ANSWER
Answered 2022-Apr-14 at 11:47Use data.table::rbindlist
:
QUESTION
With the parent-child
relationships data frame as below:
ANSWER
Answered 2022-Feb-25 at 08:17We can use ego
like below
QUESTION
I have a dataset of patient visits at a clinic. Each individual patient can visit on multiple occasions. Each patient is identified by a study_id and each visit by an illness_id. I want to iteratively filter the dataframe so that a visit that occurs within 28 days of a previous visit is removed.
I cannot simply calculate the interval between all visits and then remove those which occur within 28 days. The intervals need to be calculated iteratively as the dataframe is filtered.
In the example below you can see patient 0003 presented three times. Visit 1 is always retained. Visit 2 should be removed as it occurred 7 days after Visit 1. Once Visit 2 is removed, Visit 3 would occur 29 days after Visit 1 and so should be retained. However if I calculate all the intervals and then filter out any visits with an interval of 28 days or less, both Visits 2 and 3 would be removed (because Visit 2 occurred 7 days after Visit 1 and Visit 3 occurred 22 days after Visit 2).
study_id illness_id illness_date 0001 000103/12/2007 2007/12/03 0002 000224/03/2008 2008/03/24 0002 000226/04/2008 2008/04/26 0002 000217/07/2008 2008/07/17 0002 000221/08/2008 2008/08/21 0002 000225/08/2008 2008/08/25 0003 000329/09/2008 2008/09/29 0003 000306/10/2008 2008/10/06 0003 000328/10/2008 2008/10/28The correctly filtered dataframe should be:
study_id illness_id illness_date 0001 000103/12/2007 2007/12/03 0002 000224/03/2008 2008/03/24 0002 000226/04/2008 2008/04/26 0002 000217/07/2008 2008/07/17 0002 000221/08/2008 2008/08/21 0003 000329/09/2008 2008/09/29 0003 000328/10/2008 2008/10/28Thanks for any help - I am new to R and am struggling to get my head around iteration and loops. If there is a simple solution involving dplyr filter that would be great.
IN RESPONSE TO SOME SUGGESTIONS BELOW I AM POSTING ANOTHER EXAMPLE TO TRY AND MAKE THE QUESTION CLEARER
The 'comparator' row cannot be fixed at the first visit for each patient. It needs to roll through the dataframe as the filtering is done iteratively. Sorry if this was not clear in the OP. Here is an eg where rows 2, 3 and 5 should be removed, and rows 1, 4 and 6 should be retained.
Row 2 is 8 days after row 1, so is removed. Row 3 is 26 days after row 1, so is removed. Row 4 is 41 days after row 1, so is retained and becomes the comparator for subsequent visits for this patient. Row 5 is 6 days after row 4, so is removed. Row 6 is 31 days after row 4 so is retained and becomes the comparator for subsequent visits for this patient.
study_id illness_id illness_date 0001 000119/12/2007 19/12/2007 0001 000127/12/2007 27/12/2007 0001 000114/01/2008 14/01/2008 0001 000129/01/2008 29/01/2008 0001 000104/02/2008 04/02/2008 0001 000129/02/2008 29/02/2008Here is the v elegant solution provided by @sbarbit - sincere thanks!!
...ANSWER
Answered 2022-Feb-18 at 13:21This should do the trick:
QUESTION
This might seem like a silly question, but for the life of me I cannot figure it out so appreciate any help.
I am trying to pass .x (from purrr::map) to a function so in the end I get a list of functions (I will use this eventually to pass a list of column definitions with functions inside to reactable). This is a very basic example of what I want to do:
...ANSWER
Answered 2022-Feb-12 at 13:09You can get what you want by using a wrapper function to create a new function. Within the wrapper, create an empty function, set the args to the value x
passed in (so if x="a"
, with give the equivalent of alist(a=)
), and then also parse a string to the body. Then the returned list will be the functions you want.
QUESTION
I have problems with saving files using looping. I first wrote a function which works well without loop. However, it always fail with loop. Can anybody tell me the reason?
...ANSWER
Answered 2022-Jan-21 at 07:57The warning about condition has length
is because you are passing path=path
which is length 2, and the file=
argument of write.csv
expects length 1.
You think that you're using walk
to iterate over values, but you need to use a function or ~
quasi-function.
This works:
QUESTION
I can't get my head around the following problem.
Assuming the follwoing data:
...ANSWER
Answered 2022-Jan-14 at 12:56@tmfmnk provided an awesome answer and they deserve full credit (NOT ME)
Below is the same code from their comment (for more visibility, while also setting an initial value)
QUESTION
This question is somehow related but not depending to this Can you color an adjacent cell in gt table in r?:
I am explicitly looking for the modification of the part of my code!
I have this example dataset with colors of the background of mpg
column depending on the values applied with html.
ANSWER
Answered 2022-Jan-16 at 09:28One option would be prismatic::best_contrast
. By default it will not use pure white
so we have to set the colors:
QUESTION
When using purrr::reduce()
to left join a large list of data frames the default is to names the columns with .x, .y, .x.x, .y.y etc..
Is it possible to change this behaviour to obtain column names with numeric endings? e.g.
.1, .2, .3, .4 ?
...ANSWER
Answered 2022-Jan-13 at 13:07You can rename the columns first, and then merge:
QUESTION
I have a dataframe of timeseries data, df1
, that I need to extract a number of 'windows' from in R. The start- and end-points for the windows I need are in two columns of a separate dataframe, df2
. The values for the start- and end-points correspond to the rownumbers of the windows required.
In the example below I am part of the way to a solution but currently only the first window is extracted. How do I amend this example to extract all four windows? Could this be a case for purrr?
...ANSWER
Answered 2022-Jan-09 at 15:38Maybe try this approach with purrr::map2
QUESTION
Is there a way to do a Levene Test via the map()
function from the purrr
package? Or is there another simple way to compute a Levene Test over various variables?
My data frame contains various factor and numeric columns, so I tried with map_if()
, which works fine, e.g., for Shapiro tests. However, I do not know how to specify the formula. I want to test all my numeric variables against the "Treatment" factor.
ANSWER
Answered 2021-Dec-22 at 21:18The issue is that map
loops over the columns and it is no longer a data.frame whereas levene_test
expects a data.frame/tibble
. According to ?levene_test
data - a data frame for evaluating the formula or a model
therefore, instead of using map_if
directly, select
the columns that are numeric (select(where(is.numeric))
), get the column names (names
), loop over those in map
, select
only the 'Treatment' and the looped column from the data, create the formula with reformulate
and apply levene_test
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install purrr
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page