purrr

Applying function (ks.test) between two data frames colum-wise in R

你说的曾经没有我的故事 提交于 2019-12-30 07:06:10
问题 My simple question is: How do you do a ks.test between two data frames column by column? Eg. We have two data frames: D1 <- data.frame(D$Ag, D$Al, D$As, D$Ba, D$Be, D$Ca, D$Cd, D$Co, D$Cu, D$Cr) D2 <- data.frame(S$Ag, S$Al, S$As, S$Ba, S$Be, S$Ca, S$Cd, S$Co, S$Cu, S$Cr) Note: this is just an example - real case would include much more columns and they contain concentrations of a certain element in a specific location. Now i would like to run a ks.test between the two data frames : ks.test(D

R: Unlist list to columns in dataframe

和自甴很熟 提交于 2019-12-25 18:25:56
问题 I have a data frame which is made of up: Var1 Var2 Var3 a b nested list Nested list = ####test: apple: a pear: b ####test2: apple: a orange: c Is there a simple way to turn the nested list in v3 of each row into further columns, with the list element names as the variable names and each list value in each cell? e.g. Var1 Var2 Var3 apple pear orange a b nested list a b NULL 回答1: maybe you are looking for tidyr::spread() . It is anyways hard to try other things without a reproducible example.

Applying functions to nested dataframes with map

旧时模样 提交于 2019-12-25 08:13:08
问题 I am having an issue with nesting and mapping that I am not sure how to get around. I have a tibble with nested dataframes, as follows: > x # A tibble: 18 × 3 event.no data dr.dur <dbl> <list> <int> 1 1 <tibble [7 × 4]> 7 2 4 <tibble [123 × 4]> 123 3 5 <tibble [9 × 4]> 9 4 7 <tibble [14 × 4]> 14 5 10 <tibble [19 × 4]> 19 6 11 <tibble [220 × 4]> 220 7 12 <tibble [253 × 4]> 253 8 14 <tibble [153 × 4]> 153 9 15 <tibble [28 × 4]> 28 10 17 <tibble [169 × 4]> 169 11 18 <tibble [7 × 4]> 7 12 19

Using pmap to apply different regular expressions to different variables in a tibble?

天涯浪子 提交于 2019-12-25 04:13:51
问题 I'm trying to apply different regular expressions to different variables in a tibble. For example, I've made a tibble listing 1) the variable name I want to modify, 2) the regex I want to match, and 3) the replacement string. I'd like to apply the regex/replacement to the variable in a different data frame. So my "configuration" tibble looks like this: test_config <- dplyr::tibble( string_col = c("col1", "col2", "col3", "col4"), pattern = c("^\\.$", "^NA$", "^NULL$", "^$"), replacement = c(""

Add counts to legend labels

天涯浪子 提交于 2019-12-25 01:25:44
问题 I am trying to add the counts/percentages to legend labels of a pie chart. Pie charts are terrible, I know, but that's not the point of this post. I would like to paste the values of "Count" to the "Wound.Type" label on the legend but can't figure out how to access the Counts for each iteration of the following code. The goal would be something along the lines of "Laceration 5" or whatever the count is. I have tried ".~Count" and ".~Wound.Type", ".$Count" and ".$Wound.Type" but I don't

map2() function in pipe

人走茶凉 提交于 2019-12-25 00:13:46
问题 I got a tibble results as below > df # A tibble: 6 x 5 terms results R.squared minP maxP <dbl> <list> <dbl> <dbl> <dbl> 1 11 <tibble [6 x 9]> 0.589 0.269 0.939 2 10 <tibble [49 x 9]> 0.589 0.181 0.999 3 9 <tibble [200 x 9]> 0.589 0.0655 1.000 4 8 <tibble [527 x 9]> 0.585 0.000154 0.997 5 7 <tibble [972 x 9]> 0.565 0.0000607 0.998 6 6 <tibble [1,273 x 9]> 0.542 0.000000977 0.998 There are some modeling information save in the <list> tibble results , which has several columns with names like

Generate self reference key within the table using R mutate in a dataframe

最后都变了- 提交于 2019-12-24 15:23:04
问题 I have an input table with 3 columns (Person_Id, Visit_Id (unique Id for each visit and each person) and Purpose) as shown below. I would like to generate another new column which provides the immediate preceding visit of the person (ex: if person has visited hospital with Visit Id = 2, then I would like to have another column called "Preceding_visit_Id" which will be 1 (ex:2, if visit id = 5, preceding visit id will be 4). Is there a way to do this in a elegant manner using mutate function?

Generalize a for-loop for use in a custom function

时光总嘲笑我的痴心妄想 提交于 2019-12-24 10:48:08
问题 Using the for-loop below I can create a list of all managers above a given employee (essentially a list of an employee's manager, her manager's manager, etc.) library(dplyr) library(tidyr) library(purrr) # Create test data ds <- tibble( emp_id = c("001", "002", "003", "004", "005"), mgr_id = c("002", "004", "004", "005", NA) ) # Hardcoded for-loop example mgr_ids_above <- vector("list", length = 5) id <- "001" for (i in seq_along(mgr_ids_above)) { mgr_ids_above[[i]] <- ds$mgr_id[ds$emp_id ==

Applying purrr::map over each of a vector of characters

≡放荡痞女 提交于 2019-12-24 10:04:28
问题 I'm working on trying to understand the purrr::map function a little better. Let's say I have a simple vector of characters, and I want to run some function that outputs a data frame using each character as an input. Here's a toy example animals <- c('sheep', 'cow', 'horse') make_df <- function(x){ data.frame(r1 = rnorm(1:5), r2 = rnorm(1:5), an = x) } Here's what make_df > make_df('sheep') r1 r2 an -0.18069698 -0.4767575 sheep 0.09580225 0.2785548 sheep -0.74701529 0.2673391 sheep -1

purrr::map_int: Can't coerce element 1 from a double to a integer

我是研究僧i 提交于 2019-12-24 07:26:18
问题 I am having the weirdest bug with map_int from the purrr package. # Works as expected purrr::map_int(1:10, function(x) x) #> [1] 1 2 3 4 5 6 7 8 9 10 # Why on earth is that not working? purrr::map_int(1:10, function(x) 2*x) #> Error: Can't coerce element 1 from a double to a integer # or that? purrr::map_int(1:10, round) #> Error: Can't coerce element 1 from a double to a integer Created on 2019-03-28 by the reprex package (v0.2.1) I run 3.5.2 in rocker container (Debian) with the latest