-2

I am new to R, and I apologize if I make any blunders or mistakes I might make.

Below is the code I have used, an example of my dataset, and my output results which is 503 rows by 7 columns in total. From this subset data matrix, I want to know how I can determine if there is a significant difference in the Coefficient of Variation (CV) between serotinous and non-serotinous, listed as "Y" and "N" respectively?

Eventually I want to test if there is a correlation between latitude and the CV of "Y" and "N", and determine if there is a significant difference between the two. If anybody can help a fledgling R user I would greatly appreciate it!

#Example Data#
    SPP SEROTINOUS  LATITUDE    LONGITUDE   Y00 Y01 Y02 Y03 Y04 Y05 Y06 Y07 Y08 Y09 Y10 Y11 Y12 Y13 Y14 Y15 Y16 Y17 Y18 Y19 Y20 Y21 Y22 Y23 Y24 Y25 Y26 Y27 Y28 Y29 Y30 Y31 Y32 Y33 Y34 Y35 Y36 Y37 Y38 Y39 Y40 Y41 Y42 Y43 Y44 Y45 Y46 Y47 Y48 Y49 Y50 Y51 Y52 Y53 Y54 Y55 Y56 Y57 Y58 Y59 Y60 Y61 Y62 Y63 Y64 Y65 Y66 Y67 Y68 Y69 Y70 Y71 Y72 Y73 Y74 Y75 Y76 Y77 Y78 Y79 Y80 Y81 Y82 Y83 Y84 Y85 Y86 Y87 Y88 Y89 Y90 Y91 Y92 Y93
1   ABIES           N   53.00   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    4.2 0.0 1.1 0.0 0.0 1.1 0.0 3.3 0.0 3.3 0.0 1.0 2.3 1.0 1.0 0.0 3.0 0.0 0.0 1.0 3.3 0.0 0.0 3.6 4.3 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
2   ABIES AMABILIS  N   48.40   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    15.0    10.0    0.0 0.0 4.0 0.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
3   ABIES AMABILIS  N   48.40   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    0.0 0.0 31.0    0.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
4   ABIES AMABILIS  N   48.00   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    0.0 0.0 24.0    0.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
5   ABIES AMABILIS  N   47.00   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    10.0    0.0 0.0 2.0 1.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
6   ABIES AMABILIS  N   46.00   121.40  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    15.0    0.0 2.0 3.0 1.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
7   ABIES AMABILIS  N   46.20   122.10  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    0.0 0.0 89.0    0.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
8   ABIES AMABILIS  N   45.40   122.00  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    20.0    0.0 1.0 6.0 0.0 0.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0
9   ABIES AMABILIS  N   45.20   121.40  -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    52.0    0.0 2.0 58.0    0.0 4.0 -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0    -1.0

#Expanded Code#
CapstoneData<-read.csv("/Users/Michael/Documents/School/Capstone/CapstoneData.csv",stringsAsFactor=F)
str(CapstoneData)
raw = list() # Create an empty list
names = list() # Create an empty list
L = list() # Create an empty list
output<-matrix(nrow=503,ncol=7)

for(i in 1:503){
  vec <-subset(CapstoneData[i,],select = Y00:Y93)
  output[i,1] <-CapstoneData$SPP[i]
  output[i,2] <-CapstoneData$SEROTINOUS[i]
  output[i,3] <-CapstoneData$LATITUDE[i]
  output[i,4] <-CapstoneData$LONGITUDE[i]

  raw[[i]] <-as.numeric(as.character(vec))
  L[[i]] <-subset(raw[[i]],raw[[i]]>0)

  output[i,5]<-round(mean(L[[i]]),2)
  output[i,6]<-(sd(L[[i]]))
  output[i,7]<-((sd(L[[i]])/mean(L[[i]])))
}
output

    #Output Results Example#
       [,1]                     [,2]   [,3]     [,4]      [,5]        [,6]                [,7]               
      [1,] "ABIES"                "N"  "53"     "122"     "2.39"      "1.30999958057203"  "0.547462511283834"
      [2,] "ABIES AMABILIS"       "N"  "48.4"   "122"     "9.67"      "5.5075705472861"   "0.569748677305459"
      .......(501 more rows not shown here)
Mlk56
  • 3
  • 2
  • 2
    Welcome to SO. It is generally advised to create [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This would include example data... I suggest you rephrase your question to focus on the **specific** programming aspect of the question you are struggling with, what you have done so far to try to solve it, and to remove all the additional background. For example, while it's nice to know that you're working on your tree species, it's not really relevant to your **programming** question. – alexwhitworth Nov 16 '15 at 19:54
  • 3
    Also, "can I calculate an analysis, a t-test, or an f-test, etc" isn't a very specific question. The answer to all of them is **yes, you can**... you need to rephrase your question to be more specific the programming question you are struggling with. – alexwhitworth Nov 16 '15 at 19:56
  • what is your expected output ,that helps people to answer – Bg1850 Nov 16 '15 at 19:58

1 Answers1

0

I'm not quite sure if I can follow your code without example data, but I think that you want something close to this:

library(dplyr)
library(tidyr)

summary_data = 
  CapstoneData %>%
  gather(sample_number, Y, Y00:Y93) %>%
  filter(Y > 0) %>%
  group_by(SPP, SEROTINOUS, LATITUDE, LONGITUDE) %>%
  summarize(coefficient_of_variation_of_Y = sd(Y)/mean(Y))

(coefficient_of_variation_of_Y ~ LATITUDE + SEROTINOUS) %>%
  lm(summary_data) %>%
  summary
bramtayl
  • 4,004
  • 2
  • 11
  • 18
  • I just included example data for the code I am using if this helps @bramtayl. – Mlk56 Nov 16 '15 at 23:44
  • Great! I've made a few edits, but I think that the code above does reasonable things, provided I change some of the serotinous N's to Y's to make two groups. Does it do what you were looking for? – bramtayl Nov 16 '15 at 23:57
  • Yes, it appears to be exactly what I was looking for! I'm just a little confused by one part, what did you mean by changing some of the serotinous N's to Y's to make two groups @bramtayl. How exactly would I do this? – Mlk56 Nov 17 '15 at 02:25
  • 1
    You don't have to do this. I just had to do it in the example data you provided because they are all N's. – bramtayl Nov 17 '15 at 02:57
  • Thank you so much for your help @bramtayl . Is it possible to graph this linear model, and graph the CV values of N and Y for a comparison? Thanks again for all your help. – Mlk56 Dec 01 '15 at 18:45