0

I am new to R. I am working on making a master cheat sheet for future use. However, I noticed that when doing LSD as follow up for ANOVA, it is made as list not as table considering one of its outputs is a data frame.

This is my data (sorry I don't know how to code them in R. I use the excel file + import dataset. Been doing that ever since)

Fertilizer  Farmer  Varieties   Yield
Fertilizer1 Farmer1 B   1.64
Fertilizer1 Farmer2 D   1.21
Fertilizer1 Farmer3 C   1.425
Fertilizer1 Farmer4 A   1.345
Fertilizer2 Farmer1 C   1.475
Fertilizer2 Farmer2 A   1.185
Fertilizer2 Farmer3 D   1.4
Fertilizer2 Farmer4 B   1.29
Fertilizer3 Farmer1 A   1.67
Fertilizer3 Farmer2 C   0.71
Fertilizer3 Farmer3 B   1.665
Fertilizer3 Farmer4 D   1.18
Fertilizer4 Farmer1 D   1.565
Fertilizer4 Farmer2 B   1.29
Fertilizer4 Farmer3 A   1.655
Fertilizer4 Farmer4 C   0.66

This is my code:

#Importing data set
## insert your code here as I have mine in excel and I don't know how to put them in R manually. 

#Anova Latin Square
modelAgri <- lm(formula = Agriculture_Data$Yield ~ Agriculture_Data$Fertilizer + Agriculture_Data$Farmer + Agriculture_Data$Varieties)
anovaagri <- anova(modelAgri)
anovaagri
#LSD
LSDagri<-LSD.test(y = modelAgri,
         trt = "Agriculture_Data$Varieties",
         DFerror = modelAgri$df.residual,
         MSerror = deviance(modelAgri)/modelAgri$df.residual,
         alpha = 0.05,
         group = TRUE,
         console = TRUE)
LSDagri

How do i turn LSDagri into a table?

dww
  • 30,425
  • 5
  • 68
  • 111
  • 1
    What kind of output do you want? As you noted, `LSDagri` is a list with a bunch of different information. There's the statistics and parameters on the whole analysis, a table of the means, and a listing of the group and yield data used. How do you want that formatted as a table? If you just want to get the means table out, that's `LSDagri$means` or `LSDagri[['means']]` – divibisan Jan 25 '22 at 17:51
  • By the way, LSD stands for "least significant difference" not "least squares". I edited yout title accordingly – dww Jan 25 '22 at 17:56

1 Answers1

0

It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample code you've attempted, listing non-base R packages, any errors/warnings received, sample data (e.g., data.frame(x=...,y=...), like the output from dput(head(dataObject))), and what type of output you are expecting. Check out these resources for writing great questions: making R questions reproducible and tagging questions.

As @divibisan stated, there are a few easy ways to change parts of this into data frames. It is more than two dimensions, though.

LSDagri[1:3]
# $statistics
#     MSerror Df     Mean      CV  t.value       LSD
#   0.0215974  6 1.335312 11.0057 2.446912 0.2542752
# 
# $parameters
#         test p.ajusted                     name.t ntr alpha
#   Fisher-LSD      none Agriculture_Data$Varieties   4  0.05
# 
# $means
#   Agriculture_Data$Yield       std r       LCL     UCL   Min   Max    Q25    Q50
# A                1.46375 0.2386900 4 1.2839503 1.64355 1.185 1.670 1.3050 1.5000
# B                1.47125 0.2095382 4 1.2914503 1.65105 1.290 1.665 1.2900 1.4650
# C                1.06750 0.4426153 4 0.8877003 1.24730 0.660 1.475 0.6975 1.0675
# D                1.33875 0.1795538 4 1.1589503 1.51855 1.180 1.565 1.2025 1.3050
#       Q75
# A 1.65875
# B 1.64625
# C 1.43750
# D 1.44125
#  

# statistics:
lsd.stat <- LSDagri[[1]] # now a data.frame
#     MSerror Df     Mean      CV  t.value       LSD
#   0.0215974  6 1.335312 11.0057 2.446912 0.2542752 


lsd.mns <- LSDagri[[3]]  # now a data.frame
#   Agriculture_Data$Yield       std r       LCL     UCL   Min   Max    Q25    Q50
# A                1.46375 0.2386900 4 1.2839503 1.64355 1.185 1.670 1.3050 1.5000
# B                1.47125 0.2095382 4 1.2914503 1.65105 1.290 1.665 1.2900 1.4650
# C                1.06750 0.4426153 4 0.8877003 1.24730 0.660 1.475 0.6975 1.0675
# D                1.33875 0.1795538 4 1.1589503 1.51855 1.180 1.565 1.2025 1.3050
#       Q75
# A 1.65875
# B 1.64625
# C 1.43750
# D 1.44125 

Update based on your comment:

If you wanted the groups, then you can call it in one of two ways with the number (5th item in the list) or by name (groups).

LSDagri$groups
#   Agriculture_Data$Yield groups
# B                1.47125      a
# A                1.46375      a
# D                1.33875      a
# C                1.06750      b 

LSDagri[[5]]
#   Agriculture_Data$Yield groups
# B                1.47125      a
# A                1.46375      a
# D                1.33875      a
# C                1.06750      b 

The row names are BADC - if you want that to be a column:

# uses tidyverse
library(tidyverse)

grps <- LSDagri$groups %>% 
  mutate(fields = rownames(.), .before = 1)
#   fields Agriculture_Data$Yield groups
# B      B                1.47125      a
# A      A                1.46375      a
# D      D                1.33875      a
# C      C                1.06750      b 

Since the row names are now a column, you can remove the row names.

rownames(grps) <- NULL
grps
#   fields Agriculture_Data$Yield groups
# 1      B                1.47125      a
# 2      A                1.46375      a
# 3      D                1.33875      a
# 4      C                1.06750      b 

Let me know if you have any other questions. By the way, this is already a data frame. The object LSDagri is a list of 5 things, one is empty, the other five are data frames.

Kat
  • 15,669
  • 3
  • 18
  • 51
  • I just need to turn this output into table ``` $groups Agriculture_Data$Yield groups B 1.47125 a A 1.46375 a D 1.33875 a C 1.06750 b ``` – Deus Sema Jan 26 '22 at 07:27