Piping histograms in dplyr (R)
By : Dove
Date : March 29 2020, 07:55 AM
it helps some times lapply To put some of my comments from above into an answer, the simplest way make a histogram of each variable is code :
# let's put them in a single plot
par(mfrow = c(1, 3))
lapply(birdsss, hist, breaks = 50) # or chain into it: birdsss %>% lapply(hist, breaks = 50)
# set back to normal
par(mfrow = c(1, 1))
par(mfrow = c(1, 3))
Map(function(x, y){hist(x, breaks = 50, main = y, xlab = y)},
birdsss,
names(birdsss))
par(mfrow = c(1, 1))
birdsss %>%
Map(function(x, y){hist(x, breaks = 50, main = y, xlab = y)},
.,
names(.))
library(purrr)
walk2(birdsss, names(birdsss), ~hist(.x, breaks = 50, main = .y, xlab = .y))
birdsss %>% walk2(names(.), ~hist(.x, breaks = 50, main = .y, xlab = .y))
library(ggplot2)
# gather to long form, so there is a variable of variables to split facets by
birdsss %>%
tidyr::gather(variable, value) %>%
ggplot(aes(value)) +
# it sets bins intead of breaks, so add 1
geom_histogram(bins = 51) +
# make a new "facet" for each value of `variable` (formerly column names), and
# use a convenient xscale instead of the same for all 3
facet_wrap(~variable, scales = 'free_x')

Aggregating while concatenating in R using dplyr piping
By : Belfastboy95
Date : March 29 2020, 07:55 AM
I wish this help you Instead of count, use summarise to paste Owner as string per nWinsType, then spread: code :
DF1 %>%
group_by(nWins, Type) %>%
summarise(Owner = paste(Owner, collapse=";")) %>%
spread(Type, Owner, fill="") %>%
as.data.frame()
# nWins A B C
#1 1 Owner A;Owner D
#2 2 Owner B
#3 4 Owner B;Owner C

dplyr piping data  difference between `.` and `.x`
By : user2861526
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The . is the basic unit of transfer for the magrittr pipelines (which dplyr imports). It contains the value coming from the pipe. The .x value is something that the tidyverse world added. It's used then you have anonymous functions created with the ~ (tilde) syntax. This calls rlang::as_function to turn that formula into a function. It's basically a short cut so rather than having to type out function(x) x+5, you can just write ~.x+5. Since functions can have more than one parameter, it can be helpful to use names for that parameter so .x refers to the first parameter (and .y the second). The as_function also allows you to use . as an alias for the first parameter. It can do this because the ~ creates a formula and magrittr doesn't generally replaces . in formulas so the mapper is free to reinterpret the .. You can see the function signature here code :
f < rlang::as_function(~.x+5)
f
# <lambda>
# function (..., .x = ..1, .y = ..2, . = ..1)
# .x + 5
# attr(,"class")
# [1] "rlang_lambda_function"

Normalizing columns in mixed numeric/nonnumeric DataFrame with tidyverse (dplyr)?
By : user3343239
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I frequently need to normalize columns of DataFrames that have a mixture of numeric and nonnumeric columns. Sometimes I know the names of the numeric columns, other times not. , First problem code :
test = df %>% mutate_if(is.numeric, ~./sum(.))
test %>% select_if(is.numeric) %>% colSums( ,na.rm = T)
test = df %>% mutate_if(is.numeric, function(x) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, function(x){ifelse(x > 0, x/sum(x), rep(0, length(x)))})
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, function(x) ifelse(sum(x)>0, x/sum(x), 0))
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, ~apply(., 2, function(x) x/sum(x)))
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0) x/sum(x))
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, function(x){ifelse(x > 0, x/sum(x), rep(0, length(x)))})
test %>% select_if(is.numeric) %>% colSums()
test = df %>% mutate_if(is.numeric, function(x) if(sum(x)>0){x/sum(x)}else{0})

get lhs object name when piping with dplyr
By : Christal
Date : March 29 2020, 07:55 AM
hop of those help? The SO answer that JBGruber links to in the comments mostly solves the problem. It works by moving upwards through execution environments until a certain variable is found, then returns the lhs from that environment. The only thing missing is the requirement that the function outputs both the name of the original data frame and the manipulated data – I gleaned the latter requirement from one of the OP's comments. For that we just need to output a list containing these things, which we can do by modifying MrFlick's answer:

