purrr

Giving a different object name for the content of the returned list of purrr::map2()

◇◆丶佛笑我妖孽 提交于 2021-01-28 07:14:27
问题 I'm trying to conduct a certain a calculation using purrr::map2() taking two different arguments. purrr::map2( .x = c(1, 3), .y = c(10, 20), function(.x, .y)rnorm(1, .x, .y) ) purrr::map2() returns a list, but I want to assign a distinct object name to each content within the list. For example, I want to name the first list [[1]] [1] -5.962716 as model1 and [[2]] [1] -29.58825 as model2 . In other words, I'd like to automate the object naming like model* <- purrr::map2[[*]] . Would anybody

R: Recursive function in deeply nested list: Accessing level of hierarchy

会有一股神秘感。 提交于 2021-01-28 06:44:29
问题 Out of interest and as a thought exercise, I was aiming to replicate the tree function from the windows command line in R. tree generates ASCII trees similar to the following example (taken from here and modified). If n is the hierarchical level, note that each file or folder has (n-1)*3 spaces, followed by +--- +---Desktop +---Documents +---Custom Office Templates +---Fiddler2 +---Captures +---Requests +---Responses +---Scripts +---Favorites +---Links I use list.files() to extract the

r piping image_annotate doesn't work as expected

你说的曾经没有我的故事 提交于 2021-01-28 06:13:03
问题 I am trying to using magick to create an animated gif from a bunch of images. It works just fine but I wanted to annotate text (basically the file name) to each image before creating the gif - and that doesn't work. I can't find the cause of the error (below) - not sure if it is the piping notation, the map function, or something else. library(purrr) library(magick) #set working directory with a couple of png's #This works: image_read("image1.png") %>% image_annotate("Text") #and this works

Cumulative aggregates within tidyverse

◇◆丶佛笑我妖孽 提交于 2021-01-28 05:51:51
问题 say I have a tibble (or data.table ) which consists of two columns: a <- tibble(id = rep(c("A", "B"), each = 6), val = c(1, 0, 0, 1 ,0,1,0,0,0,1,1,1)) Furthermore I have a function called myfun which takes a numeric vector of arbitrary length as input and returns a single number. For example, you can think of myfun as being the standard deviation. Now I would like to create a third column to my tibble (called result) which contains the outputs of myfun applied to val cumulated and grouped

R - looping function in increments of 1

依然范特西╮ 提交于 2021-01-28 04:01:30
问题 I have the following function: position_tab <- filter(Tall, Time_point == 2) %>% group_by(Object) %>% summarise(minimum = min(Pixel_pos), maximum = max(Pixel_pos)) position_tab_2 <- mutate(position_tab, midpoint = minimum + ((maximum - minimum)/2)) Which produces: Object minimum maximum midpoint 1 4 22 13 2 39 85 62 etc.. This is filtering for a given timepoint, and creating a new dataframe with the midpoint variable added. Is there a way to loop this in increments of + one, so that the

R - looping function in increments of 1

…衆ロ難τιáo~ 提交于 2021-01-28 03:44:37
问题 I have the following function: position_tab <- filter(Tall, Time_point == 2) %>% group_by(Object) %>% summarise(minimum = min(Pixel_pos), maximum = max(Pixel_pos)) position_tab_2 <- mutate(position_tab, midpoint = minimum + ((maximum - minimum)/2)) Which produces: Object minimum maximum midpoint 1 4 22 13 2 39 85 62 etc.. This is filtering for a given timepoint, and creating a new dataframe with the midpoint variable added. Is there a way to loop this in increments of + one, so that the

How to do cumulative filtering with `purrr::accumulate`?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 01:51:09
问题 I'm looking for an approach to do something like this # this doesnt work # accumulate(1:8, ~filter(mtcars, carb >= .x)) So that I can examine some summary statistics at different cutoff values. I could simply do # this works but redundant filtering is done map2(list(mtcars), 1:8, ~filter(.x, carb >= .y)) But since my data is rather large, it doesn't make sense to filter out values that were already filtered out in the step just before. In essence, this just duplicates the original dataframe a

How to replace a modifying for loop by purrr

人盡茶涼 提交于 2021-01-27 17:03:10
问题 I would like to replace a simple for loop by a purrr alternative. How can I code this and keep the original structure of my object Here is my example: my_list <- list( a = list( list( aaa = c(1:3), aab = c(4:6), aac = c(7:9)), list( aaa = c(10:12), aab = c(13:15), aac = c(16:18)), list( aaa = c(19:21), aab = c(22:24), aac = c(25:27)) )) and my original solution x <- purrr::map_lgl(my_list$a, .f = ~ !is.null(.x$aaa)) my_list1 <- my_list for (i in which(x)) { my_list1$a[[i]]$aaa <- 99 } str(my

How to replace a modifying for loop by purrr

你。 提交于 2021-01-27 16:58:27
问题 I would like to replace a simple for loop by a purrr alternative. How can I code this and keep the original structure of my object Here is my example: my_list <- list( a = list( list( aaa = c(1:3), aab = c(4:6), aac = c(7:9)), list( aaa = c(10:12), aab = c(13:15), aac = c(16:18)), list( aaa = c(19:21), aab = c(22:24), aac = c(25:27)) )) and my original solution x <- purrr::map_lgl(my_list$a, .f = ~ !is.null(.x$aaa)) my_list1 <- my_list for (i in which(x)) { my_list1$a[[i]]$aaa <- 99 } str(my

How do I use safely with coxph and subset or weights?

▼魔方 西西 提交于 2021-01-27 12:23:01
问题 I'm trying to use purrr::safely with coxph so that I can capture error messages. I've made a safe version of coxph as follows library(survival) library(purrr) coxph_safe <- safely(coxph) This works perfectly when my only inputs are the formula and data, however, if I add another input such as subset or weights, I get the following error message: simpleError in eval(substitute(subset), data, env): ..3 used in an incorrect context, no ... to look in Does anyone know how to apply safely to coxph