How to calculate slope with conditions in r

Is this what you mean?

library(tidyverse)
library(broom)

# Sample data from file
url <- "https://raw.githubusercontent.com/MohJumper/VisualAuditoryModality/master/new_by_particip.csv"
download.file(url, "data.csv")
df <- read.csv("data.csv")

df %>% 
    group_nest(Source.Name, visbility, soundvolume) %>% 
    mutate(model = map(data, ~lm(m ~ stim_ending_t, data = .x))) %>% 
    mutate(slope = map_dbl(model, ~tidy(.x)$estimate[2]))
#> # A tibble: 63 x 6
#>    Source.Name            visbility soundvolume data            model slope
#>    <fct>                      <int>       <int> <list>          <lis> <dbl>
#>  1 001_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.485
#>  2 001_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.518
#>  3 001_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.612
#>  4 002_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.691
#>  5 002_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.704
#>  6 002_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.783
#>  7 003_visual_demo1.csv           0           0 <tibble [12 × … <lm>  0.539
#>  8 003_visual_demo1.csv           0           1 <tibble [12 × … <lm>  0.428
#>  9 003_visual_demo1.csv           1           0 <tibble [12 × … <lm>  0.458
#> 10 003demo_visual_demo1.…         0           0 <tibble [12 × … <lm>  0.514
#> # … with 53 more rows

Created on 2019-07-25 by the reprex package (v0.3.0.9000)

Each topic here must be about one defined issue, I recommend you to solve your first issue and then ask this question on a new topic providing a REPRoducible EXample (reprex) and making clear what kind of graph you want to do and which are the variables you want to plot.

1 Like