purrr

Error when exporting ggplot graphs to pdf: invalid font type

余生颓废 提交于 2019-12-11 02:24:46
问题 I'm having trouble getting my plots to save using ggsave(). I keep getting this error: Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type I don't understand why I would be getting this error.. I'm not using any special type of font. I looked at these similar questions: Export to pdf not displaying properly in ggplot2 Saving ggplot graph to PDF with fonts embedded in r But they haven't worked for me. The code works fine and the plots print perfectly in

Calculate means across elements in a list

偶尔善良 提交于 2019-12-11 01:48:02
问题 I have a list like this: (mylist <- list(a = data.frame(x = c(1, 2), y = c(3, 4)), b = data.frame(x = c(2, 3), y = c(4, NA)), c = data.frame(x = c(3, 4), y = c(NA, NA)))) $a x y 1 1 3 2 2 4 $b x y 1 2 4 2 3 NA $c x y 1 3 NA 2 4 NA which is created by purrr::map() . How can I calculate the means of values in the corresponding cells? i.e. x y 1 2 3.5 2 3 4 where mean(c(1, 2, 3), na.rm = T) # = 2 mean(c(2, 3, 4), na.rm = T) # = 3 mean(c(3, 4, NA), na.rm = T) # = 3.5 mean(c(4, NA, NA), na.rm = T)

mutate_impl(.data, dots) Evaluation error: object not found

醉酒当歌 提交于 2019-12-11 01:20:00
问题 I had some working code. I had to update R (and install all packages again) and when I try to run the code again hit a wall. Here's a toy example: WORKING CODE # get cyl column mtcars %>% dplyr::select(cyl) # add 1 to all numeric mtcars %>% dplyr::mutate_if(is.numeric, ~.+1) WALL But when I try to divide all numeric columns for the cyl column I can't. mtcars %>% mutate_if(is.numeric, ~./cyl) Error in mutate_impl(.data, dots) : Evaluation error: object 'cyl' not found. By the way...this works

using rvest and purrr::map_df to build a dataframe: dealing with multiple-element tags

旧街凉风 提交于 2019-12-11 01:07:35
问题 (building on my own question and its answer by @astrofunkswag here) I am webscraping webpages with rvest and turning the collected data into a dataframe using purrr::map_df . I run into the problem that map_df selects only the first element of html tags with multiple elements. Ideally, I would like all elements of a tag to be captured in the resulting dataframe, and the tags with fewer elements to be recycled. Take the following code: library(rvest) library(tidyverse) urls <- list("https://en

Splitting data and fitting distributions efficiently

ぐ巨炮叔叔 提交于 2019-12-11 00:44:37
问题 For a project I have received a large amount of confidential patient level data that I need to fit a distribution to so as to use it in a simulation model. I am using R. The problem is that I need is to fit the distribution to get the shape/rate data for at least 288 separate distributions (at least 48 subsets of 6 variables). The process will vary slightly between variables (depending on how that variable is distributed) but I want to be able to set up a function or loop for each variable

Error: missing values and NaN's not allowed if 'na.rm' is FALSE

时间秒杀一切 提交于 2019-12-10 19:18:26
问题 Trying out multiple models chapter of #r4ds and ran into an error message at the end: Error: missing values and NaN's not allowed if 'na.rm' is FALSE In addition: Warning message: In ns(as.numeric(Month), 4) : NAs introduced by coercion with ADA_model<- function(ADA_mutiple_model){ lm(ADA ~ ns(as.numeric(Month), 4), data=ADA_mutiple_model) } ADA_mutiple_model <- ADA_mutiple_model %>% mutate(model=map(data,ADA_model)) as the code I used that creates the error. See mod3 below to see what the

Apply data frame with list-variable of multivariable functions to a data frame with function arguments

纵然是瞬间 提交于 2019-12-10 16:29:44
问题 This dataframe contains what I'll call the "data": library(tidyverse) df_d <- data_frame(key = c("cat", "cat", "dog", "dog"), value_1 = c(1,2,3,4), value_2 = c(2,4,6,8)) Here is a dataframe that I intend to use as something like a function look-up table. f is a single variable function and f2 is a multivariable function: df_f <- data_frame(key = c("cat", "dog"), f = c(function(x) x^2, function(x) sqrt(x)), f2 = c(function(x) (x[1]+x[2])^2, function(x) sqrt(x[1]+x[2]))) I can easily make a

Use Dplyr::Bind_Rows and Purrr to Selectively Bind Different Dataframes In a List of Dataframes

旧时模样 提交于 2019-12-10 16:14:59
问题 library(tidyverse) I'm attempting to use tidyverse tools to selectively bind a list of dataframes using dplyr::bind_rows(). I'll split the mtcars dataset to create a basic reproduction of my real data. Df<-mtcars%>% split(.$carb)%>% head() I can bind it together with bind_rows()... Df<-Df%>% bind_rows() But how do I selectively bind elements of the list. What I want to do is create two lists - the first binds list elements 1,3,6 while the second binds 2,4,8. I'm thinking something like... Df<

Applying ntile over nested tibbles

橙三吉。 提交于 2019-12-10 15:59:54
问题 I am trying to apply ntile over some nested tibbles but I cannot seem to get it working. Can you see where I am going wrong? data(iris) iris %>% group_by(Species) %>% mutate(quintile = ntile(Petal.Length, 5)) # This works nested_iris <- iris %>% as_tibble() %>% group_by(Species) %>% nest(.key = "data") %>% mutate(quintile = map(data, ~ntile(.x, Petal.Length, 5))) #This doesn`t work 回答1: Place the ntile inside a mutate call and update the 'data' to create a new column inside it. It may be

working with lists of models using the pipe syntax

≯℡__Kan透↙ 提交于 2019-12-10 14:47:37
问题 I often like to fit and examine multiple models that relate two variables in an R dataframe. I can do that using syntax like this: require(tidyverse) require(broom) models <- list(hp ~ exp(cyl), hp ~ cyl) map_df(models, ~tidy(lm(data=mtcars, formula=.x))) But I'm used to the pipe syntax and was hoping to be able to something like this: mtcars %>% map_df(models, ~tidy(lm(data=., formula=.x))) That makes it clear that I'm "starting" with mtcars and then doing stuff to it to generate my output.