tidyverse

combine two looping structures to obtain a matrix output

怎甘沉沦 提交于 2021-02-18 08:38:49
问题 I'm using two closely related formulas in R. I was wondering if it might be possible to combine B1 and B2 to get my desired matrix output shown below? z <- "group y1 y2 1 1 2 3 2 1 3 4 3 1 5 4 4 1 2 5 5 2 4 8 6 2 5 6 7 2 6 7 8 3 7 6 9 3 8 7 10 3 10 8 11 3 9 5 12 3 7 6" dat <- read.table(text = z, header = T) (B1 = Reduce("+", group_split(dat, group, .keep = FALSE) %>% map(~ nrow(.)*(colMeans(.)-colMeans(dat[-1]))^2))) # y1 y2 #61.86667 19.05000 (B2 = Reduce("+",group_split(dat, group, .keep =

Different x and y axis scales in multifaceted scatter ggplot2

谁都会走 提交于 2021-02-17 06:46:20
问题 I have used lemon package with ggplot2 for plotting multifaceted scatter plot with regression and confidence interval line using the following code library(tidyverse) library(lemon) #Plotting ggplot(data_calibration, aes(Observed,Predicted))+ geom_point(color="black",alpha = 1/3) + facet_rep_grid(Station ~ Method, scales="free", repeat.tick.labels = "all")+ xlab("Measured") + ylab("Predicted")+ theme_bw()+ geom_smooth(method="lm") I want to have both x and y-axis scales to be free. But I am

Pivot Wider in R

…衆ロ難τιáo~ 提交于 2021-02-16 15:28:49
问题 I have a dataframe like this rest_id task_name quarter nc 123 labeling 1 TRUE 123 labeling 2 FALSE 123 labeling 3 FALSE 123 labeling 4 FALSE 123 cooking 1 TRUE 123 cooking 2 FALSE 123 cooking 3 TRUE 123 cooking 4 FALSE 123 cleaning 1 TRUE 123 cleaning 2 FALSE 123 cleaning 3 TRUE 123 cleaning 4 FALSE I want to pivot it to look like this rest_id quarter labeling cooking cleaning 123 1 TRUE TRUE TRUE 123 2 FALSE FALSE FALSE 123 3 FALSE TRUE TRUE 123 4 FALSE FALSE FALSE I've tried this: X <-

Pivot Wider in R

十年热恋 提交于 2021-02-16 15:28:05
问题 I have a dataframe like this rest_id task_name quarter nc 123 labeling 1 TRUE 123 labeling 2 FALSE 123 labeling 3 FALSE 123 labeling 4 FALSE 123 cooking 1 TRUE 123 cooking 2 FALSE 123 cooking 3 TRUE 123 cooking 4 FALSE 123 cleaning 1 TRUE 123 cleaning 2 FALSE 123 cleaning 3 TRUE 123 cleaning 4 FALSE I want to pivot it to look like this rest_id quarter labeling cooking cleaning 123 1 TRUE TRUE TRUE 123 2 FALSE FALSE FALSE 123 3 FALSE TRUE TRUE 123 4 FALSE FALSE FALSE I've tried this: X <-

ggplot2 issue: graph text shown with weird unicode blocks

主宰稳场 提交于 2021-02-15 07:16:45
问题 I have got the following problem: When I plot anything with ggplot2 like this # Libraries library(ggplot2) # create data xValue <- 1:10 yValue <- cumsum(rnorm(10)) data <- data.frame(xValue,yValue) # Plot ggplot(data, aes(x=xValue, y=yValue)) + geom_line() The resulting graph looks like this where the text is shown in weir unicode blocks: ggplot2 graph with text issue These unicode blocks look like boxes with four numbers starting with two 0s like: # Example block ---- |00| |2C| ---- I

ggplot2 issue: graph text shown with weird unicode blocks

 ̄綄美尐妖づ 提交于 2021-02-15 07:15:12
问题 I have got the following problem: When I plot anything with ggplot2 like this # Libraries library(ggplot2) # create data xValue <- 1:10 yValue <- cumsum(rnorm(10)) data <- data.frame(xValue,yValue) # Plot ggplot(data, aes(x=xValue, y=yValue)) + geom_line() The resulting graph looks like this where the text is shown in weir unicode blocks: ggplot2 graph with text issue These unicode blocks look like boxes with four numbers starting with two 0s like: # Example block ---- |00| |2C| ---- I

Writing a custom case_when function to use in dplyr mutate using tidyeval

萝らか妹 提交于 2021-02-11 13:32:12
问题 I'm trying to write a custom case_when function to use inside dplyr. I've been reading through the tidyeval examples posted in other questions, but still can't figure out how to make it work. Here's a reprex: df1 <- data.frame(animal_1 = c("Horse", "Pig", "Chicken", "Cow", "Sheep"), animal_2 = c(NA, NA, "Horse", "Sheep", "Chicken")) translate_title <- function(data, input_col, output_col) { mutate(data, !!output_col := case_when( input_col == "Horse" ~ "Cheval", input_col == "Pig" ~ "Рorc",

Facet wrap of a lollipop plot

一世执手 提交于 2021-02-11 12:49:35
问题 I'm trying to make multiple lollipop plots using a facet wrap, like in the third code block / example and picture below on this page. However, I can't get the code example to work. Can you please help me see where it is written incorrectly (if at all)? The data: set.seed(1) data <-as.data.frame(matrix( sample( 2:20 , 40 , replace=T) , ncol=10)) colnames(data) <- c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" ) data <-rbind(rep

Tidyverse: This tidyselect interface doesn't support predicates yet

巧了我就是萌 提交于 2021-02-11 08:01:52
问题 I am using R and exercising using the dslabs data for murders in the USA. As follows, library(dslabs) data("murders") library(tidyverse) murders <- mutate(murders, pop_in_millions = population / 10^6) murders <- mutate(murders, rate = total/population * 100000) murders <- mutate(murders, rank(-rate)) select(murders, state, rank) Error: This tidyselect interface doesn't support predicates yet. i Contact the package author and suggest using eval_select() . Run rlang::last_error() to see where

Most concise way to get a value from within a tibble

﹥>﹥吖頭↗ 提交于 2021-02-11 05:56:40
问题 I'm setting up tests. I operate on test-data, then want to assure that the right value showed up in a cell in a tibble. I think there's a more concise way to this. Using the example band_instruments library(tidyverse) test_that("Musicians play instruments", { expect_equal(band_instruments %>% filter(name == "Paul") %>% pull("plays"), "bass") expect_equal({band_instruments %>% filter(name == "Keith")}$plays, "guitar") }) This works, but it's too long, too wordy. What's the most concise, yet