purrr

Simulate a random walk with purrr in R

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:16:15
问题 Can purrr simulate a random walk in fewer lines of code than base R? For example what would the following look like in purrr ? n <- 5 x <- numeric(n) for(i in 2:n){ x[i] <- x[i-1] + rnorm(1) } Edit: @Imo had exactly what I was looking for. To take this one step further I am assuming a random walk can be created and plotted with the following code tibble(x = 1:1000, y = cumsum(rnorm(1000, mean = 0))) %>% ggplot(aes(x=x,y=y))+ geom_point()+ geom_line() 来源: https://stackoverflow.com/questions

How can I find the 'average' of two columns of colors in R?

匆匆过客 提交于 2019-12-10 11:42:38
问题 I have a data frame of colors and I would like to find a way to create a third column. The third column should be comprised of a color that is a blend of the two other columns. I have an example data frame and a function that I thought would do the trick. It uses R's colorRampPalette() function and purrr::map2() . However, this returns a listed column that when unlisted is identical to the first column in the dataframe. # Create a function for colors x <- RColorBrewer::brewer.pal(11,

confusing behavior of purrr::pmap with rlang; “to quote” or not to quote argument that is the Q

安稳与你 提交于 2019-12-10 04:22:42
问题 I have a custom function where I am reading entered variables from a dataframe using rlang . This function works just fine irrespective of whether the arguments entered are quoted or unquoted. But, strangely enough, when this function is used with purrr::pmap , it works only if the argument is quoted. So I have two questions: Why does the function behavior this way? How can I make a function using rlang such that I won't have to quote the arguments even if used in purrr::pmap ? Here is a

Summing Multiple Groups of Columns

假装没事ソ 提交于 2019-12-10 02:28:54
问题 I have a situation where my data frame contains the results of image analysis where the columns are the proportion of a particular class present in the image, such that an example dataframe class_df would look like: id A B C D E F 1 0.20 0.30 0.10 0.15 0.25 0.00 2 0.05 0.10 0.05 0.30 0.10 0.40 3 0.10 0.10 0.10 0.20 0.20 0.30 Each of these classes belongs to a functional group and I want to create new columns where the proportions of each functional group are calculated from the classes. An

Does a multi-value purrr::pluck exist?

丶灬走出姿态 提交于 2019-12-09 17:33:13
问题 Seems like a basic question and perhaps I'm just missing something obvious ... but is there any way to pluck a sublist (with purrr )? More specifically, here's an initial list: l <- list(a = "foo", b = "bar", c = "baz") And I want to return a new (sub-)list with only elements a and b . Normally, I'd just do 'base' R sub-listing: l[c("a", "b")] But this doesn't provide the nice .default handling of pluck . My understanding is that pluck 'replaces' [[ , but is there a purrr equivalent for

Using purrr::map to iterate linear model over columns in data frame

北战南征 提交于 2019-12-09 17:14:33
问题 I am trying to do an exercise to become more familiar with how to use the map function in purrr. I am creating some random data (10 columns of 10 datapoints) and then I wanted to use map to perform a series of regressions (i.e. lm(y ~ x, data = )) over the resulting columns in the data frame. If I just repeatedly use the first column as 'y', I want to perform 10 regressions with each column from 1 to 10 as 'x'. Obviously the results are unimportant - it's just the method. I want to end up

how to compute rowsums using tidyverse

南笙酒味 提交于 2019-12-09 06:31:51
问题 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? 回答1: 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

How to name a dataframe so that I can look for it within a list

筅森魡賤 提交于 2019-12-08 19:45:32
I have a function that returns a dataframe. I use this function with furrr::future_map2 so that I get a list with several dataframes. What I want is the ability to use the name input in the function to name the dataframe so that I can search the return list by name. example test <- function(x, name){ require(tidyverse) z <- data.frame(x+1) %>% stats::setNames(., "a") return(z) } furrr::future_map2(1:3, c("a", "b", "c"), ~test(.x, .y)) The first df within the list would be a , the second b and so on The naming should be done within the function The option of names(list.return) <- vector.of.list

Extracting data from hierarchical lists of different lengths into `data.frame` using `purr`

偶尔善良 提交于 2019-12-08 13:28:58
问题 This is a direct follow up to a previous and similar question I asked on extracting a specific subset of a list of lists: Extracting data from a list of lists into its own `data.frame` with `purrr` Hence I will use the same sample dataset: l <- list(structure(list(a = -1.54676469632688, b = "s", c = "T", d = structure(list(id = 5L, label = "Utah", link = "Asia/Anadyr", score = -0.21104594634643), .Names = c("id", "label", "link", "score")), e = 49.1279871269422), .Names = c("a", "b", "c", "d"

Combine every ith element of a list of lists together using dplyr, purrr

放肆的年华 提交于 2019-12-08 06:24:43
问题 I have a list of identically structured lists as follows: test1 <- list(first = data.frame(col1 = c(1,2), col2 = c(3,4)), second = data.frame(COL1 = c(100,200), COL2 = c(300, 400))) test2 <- list(first = data.frame(col1 = c(5,6), col2 = c(7,8)), second = data.frame(COL1 = c(500,600), COL2 = c(700,800))) orig.list <- list(test1, test2) I want to: Bind the rows the first element of each nested list together, bind the rows 2nd element of each nested list together, etc. Recombine the resulting