tidyverse

tidyr; %>% group_by() mutate(foo = fill() )

南笙酒味 提交于 2019-12-25 10:12:45
问题 I'm struggling to create a new variable to indicate what letter, LET , some groups, grp , within id, id , begin with. In the following I'll illustrate my question. I have data like this, library(dplyr); library(tidyr) df <- tibble(id = rep(0:1, c(7, 10)), grp = rep(c(0,1,0,1,2), c(3,4,2,5,3)), LET = rep(c('A', 'B', 'A', 'B', 'A', 'B'), c(1,4, 3, 3, 4, 2))) #> # A tibble: 17 x 3 #> id grp LET #> <int> <dbl> <chr> #> 1 0 0 A #> 2 0 0 B #> 3 0 0 B #> 4 0 1 B #> 5 0 1 B #> 6 0 1 A #> 7 0 1 A #> 8

tidyr; %>% group_by() mutate(foo = fill() )

岁酱吖の 提交于 2019-12-25 10:11:09
问题 I'm struggling to create a new variable to indicate what letter, LET , some groups, grp , within id, id , begin with. In the following I'll illustrate my question. I have data like this, library(dplyr); library(tidyr) df <- tibble(id = rep(0:1, c(7, 10)), grp = rep(c(0,1,0,1,2), c(3,4,2,5,3)), LET = rep(c('A', 'B', 'A', 'B', 'A', 'B'), c(1,4, 3, 3, 4, 2))) #> # A tibble: 17 x 3 #> id grp LET #> <int> <dbl> <chr> #> 1 0 0 A #> 2 0 0 B #> 3 0 0 B #> 4 0 1 B #> 5 0 1 B #> 6 0 1 A #> 7 0 1 A #> 8

How to return an average of last n rows from different group (indicated by a variable)

白昼怎懂夜的黑 提交于 2019-12-25 01:54:05
问题 I've got a data like below: data <- structure(list(seq = c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L), new_seq = c(2, 2, NA, NA, NA, NA,

Parsing a CSV with irregular quoting rules using readr

[亡魂溺海] 提交于 2019-12-25 01:48:40
问题 I have a weird CSV that I can't parse with readr. Let's call it data.csv . It looks something like this: name,info,amount_spent John Doe,Is a good guy,5412030 Jane Doe,"Jan Doe" is cool,3159 Senator Sally Doe,"Sally "Sal" Doe is from New York, NY",4451 If all of the rows were like first one below the columns row – two character columns followed by an integer column – this would be easy to parse with read_csv : df <- read_csv("data.csv") However, some rows are formatted like the second one, in

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

Calculate variables using equations then use the generated values to generate new one

末鹿安然 提交于 2019-12-24 20:50:44
问题 It's hard to explain it but i'll take it step by step Let's say I've 2 cars, one following another, and I've the speed of the lead car, and I want to calculate the distance between two of them, and we can calculate the distance using multiple equations, also I know the initial speed of the following car and the distance between two of them. Following_Car_Speed = 13.68490 m/s Distance = 17.024 m Lead_Car_Speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959, 13.945661, 13

Row subsets of `Tibble` loses custom s3 class

我只是一个虾纸丫 提交于 2019-12-24 19:29:45
问题 If I extract a row from a dataframe, my custom s3 class stays: test_df = iris class(test_df) <- c("test_class", class(test_df)) class(test_df[1,]) [1] "test_class" "data.frame" But this does not work for tibbles: test_df <- as_tibble(test_df) class(test_df) <- c("test_class", class(test_df)) class(test_df[1,]) [1] "tbl_df" "tbl" "data.frame" Is there a way around this? Thanks 回答1: The answer comes from the s3 section of Hadley's Advanced R book. You have to define a class constructor function

Change multiple character columns to date

蹲街弑〆低调 提交于 2019-12-24 18:33:24
问题 I have multiple character columns (around 20) that I would like to change all to date formats and drop the time using r. I've tried loops , mutate and apply . Here is some sample data using just two columns col1 = c("2017-04-01 23:00:00", "2017-03-03 00:00:01", "2017-04-02 00:00:01") col2 = c("2017-04-10 08:41:49", "2017-04-10 08:39:48", "2017-04-10 08:41:51") df <- cbind(col1, col2) I've tried: df <- df %>% mutate(df, funs(ymd)) and df <- df %>% mutate(df, funs(mdy)) Both gave me an error. I

Conditional statement in dplyr/tidyverse function to exclude comparisons among same levels of a factor

旧街凉风 提交于 2019-12-24 05:59:45
问题 I have a dataframe like so: data = read.table(text = "region plot species 1 1A A_B 1 1A A_B 1 1B B_C 1 1C A_B 1 1D C_D 2 2A B_C 2 2A B_C 2 2A E_F 2 2B B_C 2 2B E_F 2 2C E_F 2 2D B_C 3 3A A_B 3 3B A_B", stringsAsFactors = FALSE, header = TRUE) I wanted to compare each level of plot to get a count of unique species matches among two plot comparisons. However, I do not want comparisons among the same plots (i.e. remove/do not include 1A_1A or 1B_1B or 2C_2C, ect.). The output for this example

R Shiny - How to filter by checkboxGroupInput

南笙酒味 提交于 2019-12-24 05:54:10
问题 I have set up the following Shiny code: global.R: library(shiny) library(gapminder) library(tidyverse) library(scales) ui.R: fluidPage( titlePanel("Gapminder Hierarchical Clustering of Countries"), sidebarLayout( sidebarPanel( sliderInput("numCluster", "Choose number of clusters:", 2, 6, 2), checkboxGroupInput("ContinentSelect", "Select which continents to include in the cluster analysis:", choices = levels(gapminder$continent), selected = levels(gapminder$continent)), sliderInput("numYear",