[R] Beginning R help?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 14 23:48:14 CEST 2009


Katie Miller wrote:
> Hi all,
> 
> I'm currently working through the "Beginner's Guide to R" (Zurr et al.) and
> I'm wondering about the first exercise in chapter 3:
> 
> I imported the data from BirdFluCases.txt and executed the 'names' and 'str'
> functions as follows:
> 
> Bird = read.table(file='C:\\rbook\\BirdFluCases2.txt', header=TRUE)
> names(Bird)
> 
> [1] "Year"       "Azerbaijan" "Bangladesh" "Cambodia"   "China"
> "Djibouti"   "Egypt"
>  [8] "Indonesia"  "Iraq"       "LaoPDR"     "Myanmar"    "Nigeria"
> "Pakistan"   "Thailand"
> [15] "Turkey"     "VietNam"
> 
> str(Bird)
> 
> 'data.frame':   6 obs. of  16 variables:
>  $ Year      : int  2003 2004 2005 2006 2007 2008
>  $ Azerbaijan: int  0 0 0 8 0 0
>  $ Bangladesh: int  0 0 0 0 0 1
>  $ Cambodia  : int  0 0 4 2 1 0
>  $ China     : int  1 0 8 13 5 3
>  $ Djibouti  : int  0 0 0 1 0 0
>  $ Egypt     : int  0 0 0 18 25 7
>  $ Indonesia : int  0 0 20 55 42 18
>  $ Iraq      : int  0 0 0 3 0 0
>  $ LaoPDR    : int  0 0 0 0 2 0
>  $ Myanmar   : int  0 0 0 0 1 0
>  $ Nigeria   : int  0 0 0 0 1 0
>  $ Pakistan  : int  0 0 0 0 3 0
>  $ Thailand  : int  0 17 5 3 0 0
>  $ Turkey    : int  0 0 0 12 0 0
>  $ VietNam   : int  3 29 61 0 8 5
> 
> I obtained the total number of bird flu cases in 2003 and 2005 respectively
> with:
> 
> sum(Bird[1,2:16])
> 
> [1] 4
> 
> and
> 
> sum(Bird[3, 2:16])
> 
> [1] 98
> 
> 
> I don't know how to obtain the answers for 'Which country has had the most
> cases?', 'Which country has had the least bird flu deaths?' or (using
> methods from chapter 2), 'What is the total number of bird flu cases per
> country?' and 'What is the total number of cases per year?'
> 
> The rowSums and colSums functions haven't been introduced at this point in
> the text.

Perhaps the author either

(a) wants you to explore the available functions and find rowSums and 
colSums

or

(b) thinks that it is not excessive to ask you to add six columns or 
compute the sum of each of them separately.

or

(c) has told you about apply, lapply, or similar.

-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list