purrr

Error in bind_rows_(x, .id) : Argument 1 must have names using map_df in purrr

杀马特。学长 韩版系。学妹 提交于 2019-12-03 12:09:21
I'm using the spotifyr package to scrape spotify audio features for every song of specific albums in my dataset. My issue is that my dataset consists of some artists that are not on spotify -- so they shouldn't be returning any values. My issue is that when I get to an artist that is not on spotify, I get this error: Error in bind_rows_(x, .id) : Argument 1 must have names I've tried wrapping the function in tryCatch to get NA for each column of the problematic row, but it doesn't seem to work. Here's an example of my code (FYI, you need to get API access from spotify's website to run the

R: dplyr solution for for-loop with initial conditions set

徘徊边缘 提交于 2019-12-03 09:00:31
I have a data which has 40 days of the year and some data set.seed(123) df <- data.frame(day = 1:40,rain = runif(40,min = 0, max = 3), petc = runif(40, min = 0.3, max = 8),swc = runif(40, min = 27.01, max = 117.43)) I want to calculate another variable called aetc for each day which is calculated as follows: SW.ini <- 2 # setting some initial values SW.max <- 5 SW.min <- 0 For day 1, 1) Determine a variable called PAW(day1) = SW.ini + rain(day1) 2) If PAW(day1) >= SWC(day1), aetc(day1) = petc(day1) ; If `PAW(day1) < SWC(day1), aetc(day1) = PAW(day1)/SWC(day1) * petc(day1)` 3) Check if aetc

How to use purrr::pmap to plot multiple ggplot in nested.data.frame

非 Y 不嫁゛ 提交于 2019-12-03 08:56:08
问题 I have some questions about purrr::pmap to make multiple plots of ggplot in nested.data.frame. I can run below code without problem by using purrr::map2 and I can make multiplots(2 plots) in nested.data.frame. As a example, I used the iris dataset in R. library(tidyverse) iris0 <- iris iris0 <- iris0 %>% group_by(Species) %>% nest() %>% mutate(gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point())) %>% mutate(gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length,

how to compute rowsums using tidyverse

∥☆過路亽.° 提交于 2019-12-03 07:57:27
I did mtcars %>% by_row(sum) but got the message: by_row() is deprecated; please use a combination of: tidyr::nest(); dplyr::mutate(); purrr::map() My naive approach is this mtcars %>% group_by(id = row_number()) %>% nest(-id) %>% mutate(hi = map_dbl(data, sum)) Is there a way to do it without creating an "id" column? Is this what you are looking for? mtcars %>% mutate(rowsum = rowSums(.)) Output: mpg cyl disp hp drat wt qsec vs am gear carb rowsum 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 328.980 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 329.795 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1

How to use purrr::pmap to plot multiple ggplot in nested.data.frame

风流意气都作罢 提交于 2019-12-03 06:09:10
I have some questions about purrr::pmap to make multiple plots of ggplot in nested.data.frame. I can run below code without problem by using purrr::map2 and I can make multiplots(2 plots) in nested.data.frame. As a example, I used the iris dataset in R. library(tidyverse) iris0 <- iris iris0 <- iris0 %>% group_by(Species) %>% nest() %>% mutate(gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point())) %>% mutate(gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Width)) + geom_point())) %>% mutate(g = purrr::map2(gg1, gg2, ~ gridExtra::grid.arrange(.x, .y)))

How to fork/parallelize process in purrr::pmap

谁说我不能喝 提交于 2019-12-03 03:46:13
I have the following code that does serial processing with purr::pmap library(tidyverse) set.seed(1) params <- tribble( ~mean, ~sd, ~n, 5, 1, 1, 10, 5, 3, -3, 10, 5 ) params %>% pmap(rnorm) #> [[1]] #> [1] 4.373546 #> #> [[2]] #> [1] 10.918217 5.821857 17.976404 #> #> [[3]] #> [1] 0.2950777 -11.2046838 1.8742905 4.3832471 2.7578135 How can I parallelize (fork) the process above so that it runs faster and produces identical result? Here, I use rnorm for illustration purpose, in reality I have a function that does heavy duty work. It needs parallelizing. I'm open to non-purrr (non-tidyverse)

Use input of purrr's map function to create a named list as output in R

前提是你 提交于 2019-12-03 02:02:30
I am using the map function of the purrr package in R which gives as output a list. Now I would like the output to be a named list based on the input. An example is given below. input <- c("a", "b", "c") output <- purrr::map(input, function(x) {paste0("test-", x)}) From this I would like to access elements of the list using: output$a Or output$b We just need to name the list names(output) <- input and then extract the elements based on the name output$a #[1] "test-a" If this needs to be done using tidyverse library(tidyverse) output <- map(input, ~paste0('test-', .)) %>% setNames(input) The

Row-wise iteration like apply with purrr

风流意气都作罢 提交于 2019-12-02 17:39:47
How do I achieve row-wise iteration using purrr::map? Here's how I'd do it with a standard row-wise apply. df <- data.frame(a = 1:10, b = 11:20, c = 21:30) lst_result <- apply(df, 1, function(x){ var1 <- (x[['a']] + x[['b']]) var2 <- x[['c']]/2 return(data.frame(var1 = var1, var2 = var2)) }) However, this is not too elegant, and I would rather do it with purrr. May (or may not) be faster, too. You can use pmap for row-wise iteration. The columns are used as the arguments of whatever function you are using. In your example you would have a three-argument function. For example, here is pmap

How to use walk to silently plot ggplot2 output with purrr

本秂侑毒 提交于 2019-12-01 15:56:53
I am trying to understand how to use walk to silently (without printing to the console) return ggplot2 plots in a pipeline. library(tidyverse) # EX1: This works, but prints [[1]], [[2]], ..., [[10]] to the console 10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map(~ data.frame(.x)) %>% map(~ ggplot(., aes(x, y)) + geom_point()) # EX2: This does not plot nor print anything to the console 10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map(~ data.frame(.x)) %>% walk(~ ggplot(., aes(x, y)) + geom_point()) # EX3: This errors: Error in obj_desc(x) : object 'x' not found 10 %>% rerun(x = rnorm(5), y =

Function for Tidy chisq.test Output for Visualizing or Filtering P-Values

雨燕双飞 提交于 2019-12-01 11:39:48
For data... library(productplots) library(ggmosaic) For code... library(tidyverse) library(broom) I'm trying to create tidy chisq.test output so that I can easily filter or visualize p-values. I'm using the "happy" dataset (which is included with either of the packages listed above) For this example, if I wanted to condition the "happy" variable on all other variables,I would isolate the categorical variables (I'm not going to create factor groupings out of age, year, etc, for this example), and then run a simple function. df<-happy%>%select(-year,-age,-wtssall) lapply(df,function(x)chisq.test