tidyverse

assigning `pmap` output to dataframes with pattern names

試著忘記壹切 提交于 2019-12-13 02:58:05
问题 I am using pmap to run the same function across multiple dataframes and wish to assign each element of the output list to a separate object with a name that has a pattern. But I can't figure out how to do this. For example, here is a minimal example where I am computing quantiles for three different variables- # function call purrr::pmap(.l = list( x = list(iris$Sepal.Length, mtcars$wt, anscombe$y4), probs = list(seq(0, 1, 0.10)), na.rm = list(TRUE) ), .f = stats::quantile) # output #> [[1]]

Calculate rowwise maximum from columns that have changing names

别等时光非礼了梦想. 提交于 2019-12-13 02:57:42
问题 I have the following objects: s1 = "1_1_1_1_1" s2 = "2_1_1_1_1" s3 = "3_1_1_1_1" Please note that the value of s1, s2, s3 can change in another example. I then have the follwoing data frame: set.seed(666) df = data.frame(draw = c(1,2,3,4,1,2,3,4,1,2,3,4), resp = c(1,1,1,1,2,2,2,2,3,3,3,3), "1_1_1_1_1" = runif(12), "2_1_1_1_1" = runif(12), "3_1_1_1_1" = runif(12)). Please note that the column names of may data frame will change based on the values of s1,s2,s3. I now want to achieve the

pkgsearch: Error in as.POSIXct.default(date): do not know how to convert 'date' to class “POSIXct”

六月ゝ 毕业季﹏ 提交于 2019-12-13 02:47:05
问题 The R package pkgsearch throws the following error when using select command from dplyr : Error in as.POSIXct.default(date): do not know how to convert 'date' to class "POSIXct" Any help, please. library(tidyverse) #> Registered S3 methods overwritten by 'ggplot2': #> method from #> [.quosures rlang #> c.quosures rlang #> print.quosures rlang library(pkgsearch) PkgMYaseen208 <- pkg_search( query = "Yaseen" , format = c("short", "long")[1] , from = 1 , size = 25 ) %>% arrange(package)

Spread Data in R [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-12 20:33:54
问题 This question already has an answer here : Spread with duplicate identifiers (using tidyverse and %>%) [duplicate] (1 answer) Closed last month . I am trying to spread a single column in an R dataframe. I have reviewed many posts on SO, but cant get my solution to work because most solutions seem to require a formula (count, mean, sum, etc). I am simply looking to spread a column of characters. For example: library(tidyverse) school<- c("univ1", "univ2","univ1", "univ2","univ1" ) student<-c(

Using fct_relevel over a list of variables using map_at

為{幸葍}努か 提交于 2019-12-12 17:32:34
问题 I have a bunch of factor variables that have the same levels, and I want them all reordered similarly using fct_relevel from the forcats package. Many of the variable names start with the same characters ("Q11A" to "Q11X", "Q12A" to "Q12X", "Q13A" to "Q13X", etc.). I wanted to use the starts_with function from dplyr to shorten the task. The following error didn't give me an error, but it didn't do anything either. Is there anything I'm doing wrong? library(dplyr) library(purrr) library

Processing arbitrary hierarchies recursively with purrr

↘锁芯ラ 提交于 2019-12-12 12:58:20
问题 Suppose that I wanted to prune a tree consisting of a hierarchy of nested lists in R, based on some particular criterion. I can do this "easily" enough using lapply : # Based an example from the NetworkD3 documentation # https://christophergandrud.github.io/networkD3/ URL <- paste0( "https://cdn.rawgit.com/christophergandrud/networkD3/", "master/JSONdata//flare.json") flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE) # Leaf nodes have a "size" attribute. Let's say we want to # prune

Apply a list to a function that outputs a dataframe

柔情痞子 提交于 2019-12-12 10:04:34
问题 My single-argument function outputs a dataframe library(tidyverse) myfun <-function(x) {mtcars %>% filter_(x) %>% group_by(cyl) %>% summarise(mean(disp), mean(drat)) %>% mutate(group=x)} When feeding a single-argument into this function, it outputs, as expected, a dataframe: myfun('mpg>15') cyl mean(disp) mean(drat) group 4 105 4.07 mpg>15 6 183 3.59 mpg>15 8 105 3.20 mpg>15 How to apply such a function to a list of arguments so that the output is one dataframe combining all the results over

Gathering specific pairs of columns into rows by dplyr in R [duplicate]

一笑奈何 提交于 2019-12-12 09:39:27
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed last year . I am trying to convert a data frame from wide to long format by gathering specific pairs of columns of which example is shown below: An example of data frame df <- data.frame(id=c(1,2,3,4,5), var=c("a","d","g","f","i"),a1=c(3,5,1,2,2), b1=c(2,4,1,2,3), a2=c(8,1,2,5,1), b2=c(1,6,4,7,2), a3=c(7,7,2,3,1), b3=c(1,1,4,9,6)) Initial

Extract longest word in string

試著忘記壹切 提交于 2019-12-12 08:53:11
问题 I would like to find and extract the longest word of a string, if possible using a tidyverse package. library(tidyverse) tbl <- tibble(a=c("ab cde", "bcde f", "cde fg"), b=c("cde", "bcde", "cde")) tbl # A tibble: 3 x 1 a <chr> 1 ab cde 2 bcde f 3 cde fg The result I am looking for is: # A tibble: 3 x 2 a b <chr> <chr> 1 ab cde cde 2 bcde f bcde 3 cde fg cde The closest post to the question I have found is this: longest word in a string. Does anyone have an idea for an even simpler way? 回答1:

Find variable combinations that makes Primary Key in R

﹥>﹥吖頭↗ 提交于 2019-12-12 07:58:21
问题 Here is my toy dataframe. df <- tibble::tribble( ~var1, ~var2, ~var3, ~var4, ~var5, ~var6, ~var7, "A", "C", 1L, 5L, "AA", "AB", 1L, "A", "C", 2L, 5L, "BB", "AC", 2L, "A", "D", 1L, 7L, "AA", "BC", 2L, "A", "D", 2L, 3L, "BB", "CC", 1L, "B", "C", 1L, 8L, "AA", "AB", 1L, "B", "C", 2L, 6L, "BB", "AC", 2L, "B", "D", 1L, 9L, "AA", "BC", 2L, "B", "D", 2L, 6L, "BB", "CC", 1L) How can I get the combination of a minimum number of variables that uniquely identify the observations in the dataframe i.e