tidyverse

counting values after and before change in value, within groups, generating new variables for each unique shift

◇◆丶佛笑我妖孽 提交于 2019-11-30 23:54:17
I am looking for a way to, within id groups, count unique occurrences of value shifts in TF in the data data tbl . I want to count both forward and backwards from when TF changes between 1 and 0 or o and 1 . The counting is to be stored in a new variable PM## , so that the PM## s holds each unique shift in TF , in both plus and minus. The MWE below leads to an outcome with 7 PM, but my production data can have 15 or more shifts. If a TF values does not change between NA 's I want to mark it 0 . This question is similar to a question I previously asked , but the last part about TF standing

using tidyverse; counting after and before change in value, within groups, generating new variables for each unique shift

会有一股神秘感。 提交于 2019-11-30 17:46:13
I am looking for a tidyverse -solution that can count occurrences of unique values of TF within groups, id in the data data tbl . When TF changes I want to count both forward and backwards from that point. This counting should be stored in a new variable PM## , so that PM## holds both plus and minus to each unique shift in TF . This question is similar to a question I previously asked , but here I am specifically looking for a solution using tidyverse tools. Uwe provided an elegant answer to the inital question using data.table here . If this question violates any SO policies please let me

Filter by multiple patterns with filter() and str_detect()

隐身守侯 提交于 2019-11-30 17:25:21
问题 I would like to filter a dataframe using filter() and str_detect() matching for multiple patterns without multiple str_detect() function calls. In the example below I would like to filter the dataframe df to show only rows containing the letters a f and o . df <- data.frame(numbers = 1:52, letters = letters) df %>% filter( str_detect(.$letters, "a")| str_detect(.$letters, "f")| str_detect(.$letters, "o") ) # numbers letters #1 1 a #2 6 f #3 15 o #4 27 a #5 32 f #6 41 o I have attempted the

How to reorder factor levels in a tidy way?

China☆狼群 提交于 2019-11-30 14:02:06
Hi I usually use some code like the following to reorder bars in ggplot or other types of plots. Normal plot (unordered) library(tidyverse) iris.tr <-iris %>% group_by(Species) %>% mutate(mSW = mean(Sepal.Width)) %>% select(mSW,Species) %>% distinct() ggplot(iris.tr,aes(x = Species,y = mSW, color = Species)) + geom_point(stat = "identity") Ordering the factor + ordered plot iris.tr$Species <- factor(iris.tr$Species, levels = iris.tr[order(iris.tr$mSW),]$Species, ordered = TRUE) ggplot(iris.tr,aes(x = Species,y = mSW, color = Species)) + geom_point(stat = "identity") The factor line is

How to install Tidyverse on Ubuntu 16.04 and 17.04

孤街醉人 提交于 2019-11-30 13:12:42
I'm running Ubuntu 16.04 [now 17.04: see note in bold below] and R 3.4.1. I installed the latter this morning, so I presume it's the latest version. I want to install Tidyverse, which I've spent many happy hours with under Windows. But when I do install.packages("tidyverse") , I get errors about unrecognized command line options to gcc. These start when the install hits the colorspace and munsell packages. I'll show an example at the end of this post, just for munsell. I've not found anyone else who had exactly that problem on Ubuntu 16.04. But posts from people with similar problems suggest

summarise_at using different functions for different variables

别等时光非礼了梦想. 提交于 2019-11-30 05:12:30
When I use group_by and summarise in dplyr, I can naturally apply different summary functions to different variables. For instance: library(tidyverse) df <- tribble( ~category, ~x, ~y, ~z, #---------------------- 'a', 4, 6, 8, 'a', 7, 3, 0, 'a', 7, 9, 0, 'b', 2, 8, 8, 'b', 5, 1, 8, 'b', 8, 0, 1, 'c', 2, 1, 1, 'c', 3, 8, 0, 'c', 1, 9, 1 ) df %>% group_by(category) %>% summarize( x=mean(x), y=median(y), z=first(z) ) results in output: # A tibble: 3 x 4 category x y z <chr> <dbl> <dbl> <dbl> 1 a 6 6 8 2 b 5 1 8 3 c 2 8 1 My question is, how would I do this with summarise_at? Obviously for this

How to rename a column to a variable name “in a tidyverse way”

岁酱吖の 提交于 2019-11-30 03:38:30
问题 I've created a simple data frame ( dput below): date ticker value ------------------------------ 2016-06-30 A2M.ASX 0.0686 2016-07-29 A2M.ASX -0.0134 2016-08-31 A2M.ASX -0.0650 2016-09-30 A2M.ASX 0.0145 2016-10-31 A2M.ASX 0.3600 2016-11-30 A2M.ASX -0.1429 I want to change the value column's name to whatever is in my metric variable name, and I want to do it in a dplyr way. My sample data: df = structure(list(date = c("2016-06-30", "2016-07-29", "2016-08-31", "2016-09-30", "2016-10-31", "2016

using tidyverse; counting after and before change in value, within groups, generating new variables for each unique shift

折月煮酒 提交于 2019-11-30 01:17:47
问题 I am looking for a tidyverse-solution that can count occurrences of unique values of TF within groups, id in the data data tbl . When TF changes I want to count both forward and backwards from that point. This counting should be stored in a new variable PM## , so that PM## holds both plus and minus to each unique shift in TF . This question is similar to a question I previously asked, but here I am specifically looking for a solution using tidyverse tools. Uwe provided an elegant answer to

How to reorder factor levels in a tidy way?

我的梦境 提交于 2019-11-29 19:35:04
问题 Hi I usually use some code like the following to reorder bars in ggplot or other types of plots. Normal plot (unordered) library(tidyverse) iris.tr <-iris %>% group_by(Species) %>% mutate(mSW = mean(Sepal.Width)) %>% select(mSW,Species) %>% distinct() ggplot(iris.tr,aes(x = Species,y = mSW, color = Species)) + geom_point(stat = "identity") Ordering the factor + ordered plot iris.tr$Species <- factor(iris.tr$Species, levels = iris.tr[order(iris.tr$mSW),]$Species, ordered = TRUE) ggplot(iris.tr

round_any equivalent for dplyr?

老子叫甜甜 提交于 2019-11-29 14:05:42
I am trying to make a switch to the "new" tidyverse ecosystem and try to avoid loading the old packages from Wickham et al. I used to rely my coding previously. I found round_any function from plyr useful in many cases where I needed custom rounding for plots, tables, etc. E.g. x <- c(1.1, 1.0, 0.99, 0.1, 0.01, 0.001) library(plyr) round_any(x, 0.1, floor) # [1] 1.1 1.0 0.9 0.1 0.0 0.0 Is there an equivalent for round_any function from plyr package in tidyverse ? ggplot::cut_width as pointed to in one of the comments, does not even return a numeric vector, but a factor instead. So it is no