-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

2

Try:

df$Fertilizer <- as.factor(df$Fertilizer)
df$Fertilizer <- relevel(df$Fertilizer, ref = "4")
fit <- lm(Yield ~ Fertilizer, data = df)
summary(fit)
Sue
  • 36
  • 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,219
  • 14
  • 22
  • can you give me more detail? Ultimately i want to do a regression analysis.... –  May 06 '17 at 03:56
  • I edited my answer. Now you should be able to use the new data.frame in a regression analysis. – Julian Wittische May 06 '17 at 03:59
  • I made it as a new data.frame. But when i try to do a regression analysis, the error " corns<-lm(Yield~.,data = cornn) Error in model.frame.default(formula = Yield ~ ., data = cornn, drop.unused.levels = TRUE) : 'data' must be a data.frame, not a matrix or an array" came out... –  May 06 '17 at 04:06