11.07.2015 Views

Preface to First Edition - lib

Preface to First Edition - lib

Preface to First Edition - lib

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BASIC DATA MANIPULATION 13name sales profits assets10 Wal-Mart S<strong>to</strong>res 256.33 9.05 104.915 BP 232.57 10.27 177.574 ExxonMobil 222.88 20.96 166.99Another way of selecting vec<strong>to</strong>r elements is the use of a logical vec<strong>to</strong>r beingTRUE when the corresponding element is <strong>to</strong> be selected and FALSE otherwise.The companies with assets of more than 1000 billion US dollars areR> Forbes2000[Forbes2000$assets > 1000,+ c("name", "sales", "profits", "assets")]name sales profits assets1 Citigroup 94.71 17.85 1264.039 Fannie Mae 53.13 6.48 1019.17403 Mizuho Financial 24.40 -20.11 1115.90where the expression Forbes2000$assets > 1000 indicates a logical vec<strong>to</strong>rof length 2000 withR> table(Forbes2000$assets > 1000)FALSE TRUE1997 3elements being either FALSE or TRUE. In fact, for some of the companies themeasurement of the profits variable are missing. In R, missing values aretreated by a special symbol, NA, indicating that this measurement is not available.The observations with profit information missing can be obtained viaR> na_profits table(na_profits)na_profitsFALSE TRUE1995 5R> Forbes2000[na_profits,+ c("name", "sales", "profits", "assets")]name sales profits assets772 AMP 5.40 NA 42.941085 HHG 5.68 NA 51.651091 NTL 3.50 NA 10.591425 US Airways Group 5.50 NA 8.581909 Laidlaw International 4.48 NA 3.98where the function is.na returns a logical vec<strong>to</strong>r being TRUE when the correspondingelement of the supplied vec<strong>to</strong>r is NA. A more comfortable approachis available when we want <strong>to</strong> remove all observations with at least one missingvalue from a data.frame object. The function complete.cases takes adata.frame and returns a logical vec<strong>to</strong>r being TRUE when the correspondingobservation does not contain any missing value:R> table(complete.cases(Forbes2000))© 2010 by Taylor and Francis Group, LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!