1

I have a question on how to mutate the slopes of lines into a new data frame into

by category.


d1 <-read.csv(file.choose(), header = T)
d2 <- d1 %>%
  group_by(ID)%>%
  mutate(Slope=sapply(split(df,df$ID), function(v) lm(x~y,v)$coefficients["y"]))

ID  x   y
1   3.429865279 2.431363764
1   3.595066124 2.681241237
1   3.735263469 2.352182518
1   3.316473584 2.51851394
1   3.285984642 2.380211242
1   3.860793029 2.62324929
1   3.397714117 2.819543936
1   3.452997088 2.176091259
1   3.718933278 2.556302501
1   3.518566578 2.537819095
1   3.689033452 2.40654018
1   3.349160923 2.113943352
1   3.658888644 2.556302501
1   3.251151343 2.342422681
1   3.911194909 2.439332694
1   3.432584505 2.079181246
1   4.031267043 2.681241237
1   3.168733129 1.544068044
1   4.032239897 3.084576278
1   3.663361648 2.255272505
1   3.582302046 2.62324929
1   3.606585565 2.079181246
1   3.541791347 2.176091259
4   3.844012861 2.892094603
4   3.608318477 2.767155866
4   3.588990218 2.883661435
4   3.607957917 2.653212514
4   3.306753044 2.079181246
4   4.002604841 2.880813592
4   3.195299837 2.079181246
4   3.512203238 2.643452676
4   3.66878494  2.431363764
4   3.598910385 2.511883361
4   3.721810134 2.819543936
4   3.352964661 2.113943352
4   4.008109343 3.084576278
4   3.584693332 2.556302501
4   4.019461819 3.084576278
4   3.359474563 2.079181246
4   3.950256012 2.829303773

I got the error message like'replacement has 2 rows, data has 119'. I am sure that the error is derived from mutate().

Best,

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
AJI
  • 51
  • 5
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What does your data look like? What exactly do you hope to get back? It seems like the `split(df,df$ID)` is redundant based on the `group_by` and `df` doesn't appear in the other variables here. – MrFlick Apr 26 '20 at 21:53

1 Answers1

0

Once you do group_by, any function that succeeds uses on the columns in the grouped data.frame, in your case, it will only use x,y column within.

If you only want the coefficient, it goes like this:

df %>% group_by(ID) %>% summarize(coef=lm(x~y)$coefficients["y"])
# A tibble: 2 x 2
     ID  coef
  <int> <dbl>
1     1 0.437
2     4 0.660

If you want the coefficient, which means a vector a long as the dataframe, you use mutate:

df %>% group_by(ID) %>% mutate(coef=lm(x~y)$coefficients["y"])
# A tibble: 40 x 4
# Groups:   ID [2]
      ID     x     y  coef
   <int> <dbl> <dbl> <dbl>
 1     1  3.43  2.43 0.437
 2     1  3.60  2.68 0.437
 3     1  3.74  2.35 0.437
 4     1  3.32  2.52 0.437
 5     1  3.29  2.38 0.437
 6     1  3.86  2.62 0.437
 7     1  3.40  2.82 0.437
 8     1  3.45  2.18 0.437
 9     1  3.72  2.56 0.437
10     1  3.52  2.54 0.437
# … with 30 more rows
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thank you for the answers. Do you know why I got the error massgage when I used funciton()? – AJI Apr 27 '20 at 12:56
  • you are using sapply(by..), it's not the function that is causing the problem, it's the output from sapply and by are incompatible with mutate... – StupidWolf Apr 27 '20 at 12:58
  • https://www.r-bloggers.com/using-apply-sapply-lapply-in-r/ and https://dplyr.tidyverse.org/ are very different data structures.. use them interchangeably only when you know what's going on – StupidWolf Apr 27 '20 at 12:59
  • maybe check this too https://swcarpentry.github.io/r-novice-inflammation/13-supp-data-structures/ – StupidWolf Apr 27 '20 at 13:00
  • I ran the codes, but I got the same slope for the ID1 and ID4. If I want different slopes by ID, what should I do? Sorry for the keep asking questions. – AJI Apr 27 '20 at 13:20
  • ```df %>% group_by(ID) %>% mutate(coef=lm(x~y)$coefficients["y"]) ``` – AJI Apr 27 '20 at 13:38
  • This code, gives one coefficient 0.437 for all IDs ==1 and 0.660 for ID==4, what is the problem you are having.. sorry is it possible to be a bit more clear and concise about where and what is the problem? – StupidWolf Apr 27 '20 at 13:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212619/discussion-between-aji-and-stupidwolf). – AJI Apr 27 '20 at 13:50