Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 2.44 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. mapping factors to data frame
  2. >loc
  3.   cruise station     lon    lat
  4. 1    TY1      A1 53.8073 6.7836
  5. 2    TY1       3 53.7757 6.7009
  6. 3    AZ7      A1 53.7764 6.6758
  7.        
  8. >spe
  9.   cruise station     species abundance
  10. 1    TY1      A1 Ensis ensis       100
  11. 2    TY1      A1    Magelona         5
  12. 3    TY1      A1    Nemertea        17
  13. 4    TY1       3    Magelona         8
  14. 5    TY1       3     Ophelia      1200
  15. 6    AZ7      A1     Ophelia       950
  16. 7    AZ7      A1 Ensis ensis        89
  17. 8    AZ7      A1        Spio         1
  18.        
  19. cruise station     species abundance     ID
  20. 1    TY1      A1 Ensis ensis       100 STA0001
  21. 2    TY1      A1    Magelona         5 STA0001
  22. 3    TY1      A1    Nemertea        17 STA0001
  23. 4    TY1       3    Magelona         8 STA0002
  24. 5    TY1       3     Ophelia      1200 STA0002
  25. 6    AZ7      A1     Ophelia       950 STA0003
  26. 7    AZ7      A1 Ensis ensis        89 STA0003
  27. 8    AZ7      A1        Spio         1 STA0003
  28.        
  29. loc<-data.frame(cruise=c("TY1","TY1","AZ7"),station=c("A1",3,"A1"),lon=c(53.8073, 53.7757, 53.7764),lat=c(6.7836, 6.7009, 6.6758))
  30.  
  31. spe<-data.frame(cruise=c(rep("TY1",5),rep("AZ7",3)),station=c(rep("A1",3),rep(3,2),rep("A1",3)),species=c("Ensis ensis", "Magelona", "Nemertea", "Magelona", "Ophelia", "Ophelia","Ensis ensis", "Spio"),abundance=c(100,5,17,8,1200,950,89,1))
  32.        
  33. loc$ID<-paste("STA",formatC(1:nrow(loc),width=4,format="d",flag="0"),sep="")
  34.        
  35. merge(spe, loc, all.x=TRUE)
  36.  
  37.   cruise station     species abundance     lon    lat
  38. 1    AZ7      A1     Ophelia       950 53.7764 6.6758
  39. 2    AZ7      A1 Ensis ensis        89 53.7764 6.6758
  40. 3    AZ7      A1        Spio         1 53.7764 6.6758
  41. 4    TY1       3    Magelona         8 53.7757 6.7009
  42. 5    TY1       3     Ophelia      1200 53.7757 6.7009
  43. 6    TY1      A1 Ensis ensis       100 53.8073 6.7836
  44. 7    TY1      A1    Magelona         5 53.8073 6.7836
  45. 8    TY1      A1    Nemertea        17 53.8073 6.7836
  46.        
  47. unique(paste(loc$cruise, loc$station, sep="-"))
  48. [1] "TY1-A1" "TY1-3"  "AZ7-A1"
  49.        
  50. loc <- within(loc, id <- interaction(cruise, station))
  51. spe <- within(spe, id <- interaction(cruise, station))
  52.        
  53. loc$ID<-paste("STA", formatC(1:nrow(loc), width=4, format="d", flag="0"), sep="")
  54.        
  55. as.data.frame.matrix(xtabs(abundance ~ ID + species, merge(spe, loc, all.x=T)))
  56.         Ensis ensis Magelona Nemertea Ophelia Spio
  57. STA0001         100        5       17       0    0
  58. STA0002           0        8        0    1200    0
  59. STA0003          89        0        0     950    1