-2
Yield   Fertilizer
31  1
34  1
34  1
34  1
43  1
35  1
38  1
36  1
36  1
45  1
27  2
27  2
25  2
34  2
21  2
36  2
34  2
30  2
32  2
33  2
36  3
37  3
37  3
34  3
37  3
28  3
33  3
29  3
36  3
42  3
33  4
27  4
35  4
25  4
29  4
20  4
25  4
40  4
35  4
29  4

I have to divide fertilizer 1 as dummy variable F1, fertilizer 2 as a dummy variable F2 and fertilizer 3 as a dummy variable F3 and 4 as a base line.

I import this csv file into R by using read.csv function. But after that when I just use lm function it does not come as I want... What should I do for that?

2 Answers2

1

Try:

df$Fertilizer <- as.factor(df$Fertilizer)
df$Fertilizer <- relevel(df$Fertilizer, ref = "4")
fit <- lm(Yield ~ Fertilizer, data = df)
summary(fit)
Sue
  • 26
  • 1
  • 5
0

You can use the model.matrix() function to achieve that.

This should get you started:

new_df <- model.matrix(Yield ~ as.factor(Fertilizer)-1,your_df)
Julian Wittische
  • 1,139
  • 12
  • 21