R 语言是一种用于统计计算和图形的编程语言,它提供了许多函数来处理数据。在本文中,我们介绍了如何使用 R 语言处理不同格式的数据。首先,我们介绍了内置数据,这是 R 语言中的一些常用数据集,可以用来测试函数。其次,我们介绍了如何使用 R 语言的内置函数 read.table() 来读取表格式数据。表格式数据是一种常见的数据格式,它以分隔符(如空格、制表符或逗号)分隔变量。我们还介绍了如何使用 readLines() 函数来读取非表格式数据。最后,我们介绍了如何使用 write.table() 函数来输出表格式数据,以及如何使用 toJSON() 函数来输出 JSON 格式数据。
Working with data provided by R packages is a great way to learn the tools of data science, but at some point you want to stop learning and start working with your own data.
Hadley Wickham
通過輕鬆學習 R 語言:自訂函數與輕鬆學習 R 語言:函數型程式設計的介紹,終於可以鬆一口氣了!基本功部分已經告一段落,接下來我們將會有水到渠成的感覺。先前我們利用手動的方式建立單純的資料結構來學習,然而在現實生活中,R 語言的應用情形多是輸入一組資料再針對它進行清理或者分析,最後再將結果輸出成一個處理後的檔案。
內建資料
R 語言有非常豐富的內建資料,這些資料絕大多數是以資料框(data.frame)這個結構儲存,並且在開啟 R 語言之後就可以直接使用,想要知道有哪些內建資料可以使用,只要使用 data() 函數就可以在來源(Source)區塊瀏覽一份內建資料集清單。
## Data sets in package ‘datasets’:#### AirPassengers Monthly Airline Passenger Numbers 1949-1960## BJsales Sales Data with Leading Indicator## BJsales.lead (BJsales)## Sales Data with Leading Indicator## BOD Biochemical Oxygen Demand## CO2 Carbon Dioxide Uptake in Grass Plants## ChickWeight Weight versus age of chicks on different diets## DNase Elisa assay of DNase## EuStockMarkets Daily Closing Prices of Major European Stock## Indices, 1991-1998## Formaldehyde Determination of Formaldehyde## HairEyeColor Hair and Eye Color of Statistics Students## Harman23.cor Harman Example 2.3## Harman74.cor Harman Example 7.4## Indometh Pharmacokinetics of Indomethacin## InsectSprays Effectiveness of Insect Sprays## JohnsonJohnson Quarterly Earnings per Johnson & Johnson Share## LakeHuron Level of Lake Huron 1875-1972## LifeCycleSavings Intercountry Life-Cycle Savings Data## Loblolly Growth of Loblolly pine trees## Nile Flow of the River Nile## Orange Growth of Orange Trees## OrchardSprays Potency of Orchard Sprays## PlantGrowth Results from an Experiment on Plant Growth## Puromycin Reaction Velocity of an Enzymatic Reaction## Seatbelts Road Casualties in Great Britain 1969-84## Theoph Pharmacokinetics of Theophylline## Titanic Survival of passengers on the Titanic## ToothGrowth The Effect of Vitamin C on Tooth Growth in## Guinea Pigs## UCBAdmissions Student Admissions at UC Berkeley## UKDriverDeaths Road Casualties in Great Britain 1969-84## UKgas UK Quarterly Gas Consumption## USAccDeaths Accidental Deaths in the US 1973-1978## USArrests Violent Crime Rates by US State## USJudgeRatings Lawyers' Ratings of State Judges in the US## Superior Court## USPersonalExpenditure Personal Expenditure Data## UScitiesD Distances Between European Cities and Between## US Cities## VADeaths Death Rates in Virginia (1940)## WWWusage Internet Usage per Minute## WorldPhones The World's Telephones## ability.cov Ability and Intelligence Tests## airmiles Passenger Miles on Commercial US Airlines,## 1937-1960## airquality New York Air Quality Measurements## anscombe Anscombe's Quartet of 'Identical' Simple Linear## Regressions## attenu The Joyner-Boore Attenuation Data## attitude The Chatterjee-Price Attitude Data## austres Quarterly Time Series of the Number of## Australian Residents## beaver1 (beavers) Body Temperature Series of Two Beavers## beaver2 (beavers) Body Temperature Series of Two Beavers## cars Speed and Stopping Distances of Cars## chickwts Chicken Weights by Feed Type## co2 Mauna Loa Atmospheric CO2 Concentration## crimtab Student's 3000 Criminals Data## discoveries Yearly Numbers of Important Discoveries## esoph Smoking, Alcohol and (O)esophageal Cancer## euro Conversion Rates of Euro Currencies## euro.cross (euro) Conversion Rates of Euro Currencies## eurodist Distances Between European Cities and Between## US Cities## faithful Old Faithful Geyser Data## fdeaths (UKLungDeaths)## Monthly Deaths from Lung Diseases in the UK## freeny Freeny's Revenue Data## freeny.x (freeny) Freeny's Revenue Data## freeny.y (freeny) Freeny's Revenue Data## infert Infertility after Spontaneous and Induced## Abortion## iris Edgar Anderson's Iris Data## iris3 Edgar Anderson's Iris Data## islands Areas of the World's Major Landmasses## ldeaths (UKLungDeaths)## Monthly Deaths from Lung Diseases in the UK## lh Luteinizing Hormone in Blood Samples## longley Longley's Economic Regression Data## lynx Annual Canadian Lynx trappings 1821-1934## mdeaths (UKLungDeaths)## Monthly Deaths from Lung Diseases in the UK## morley Michelson Speed of Light Data## mtcars Motor Trend Car Road Tests## nhtemp Average Yearly Temperatures in New Haven## nottem Average Monthly Temperatures at Nottingham,## 1920-1939## npk Classical N, P, K Factorial Experiment## occupationalStatus Occupational Status of Fathers and their Sons## precip Annual Precipitation in US Cities## presidents Quarterly Approval Ratings of US Presidents## pressure Vapor Pressure of Mercury as a Function of## Temperature## quakes Locations of Earthquakes off Fiji## randu Random Numbers from Congruential Generator## RANDU## rivers Lengths of Major North American Rivers## rock Measurements on Petroleum Rock Samples## sleep Student's Sleep Data## stack.loss (stackloss)## Brownlee's Stack Loss Plant Data## stack.x (stackloss) Brownlee's Stack Loss Plant Data## stackloss Brownlee's Stack Loss Plant Data## state.abb (state) US State Facts and Figures## state.area (state) US State Facts and Figures## state.center (state) US State Facts and Figures## state.division (state)## US State Facts and Figures## state.name (state) US State Facts and Figures## state.region (state) US State Facts and Figures## state.x77 (state) US State Facts and Figures## sunspot.month Monthly Sunspot Data, from 1749 to "Present"## sunspot.year Yearly Sunspot Data, 1700-1988## sunspots Monthly Sunspot Numbers, 1749-1983## swiss Swiss Fertility and Socioeconomic Indicators## (1888) Data## treering Yearly Treering Data, -6000-1979## trees Girth, Height and Volume for Black Cherry Trees## uspop Populations Recorded by the US Census## volcano Topographic Information on Auckland's Maunga## Whau Volcano## warpbreaks The Number of Breaks in Yarn during Weaving## women Average Heights and Weights for American Women###### Use ‘data(package = .packages(all.available = TRUE))’## to list the data sets in all *available* packages.
## > file_path <- "https://s3-ap-northeast-1.amazonaws.com/r-essentials/friends_script.txt"
## > friends_script <- readLines(file_path)
## Warning message:
## In readLines(file_path) :
## incomplete final line found on 'https://s3-ap-northeast-1.amazonaws.com/r-essentials/friends_script.txt'
## > friends_script
## [1] "Ross: (mortified) Hi."
## [2] "Joey: This guy says hello, I wanna kill myself."
## [3] "Monica: Are you okay, sweetie?"
## [4] "Ross: I just feel like someone reached down my throat, grabbed my small intestine, pulled it out of my mouth and tied it around my neck..."
## [5] "Chandler: Cookie?"
## [6] "Monica: (explaining to the others) Carol moved her stuff out today."
## [7] "Joey: Ohh."
## [8] "Monica: (to Ross) Let me get you some coffee."
## [9] "Ross: Thanks."
## [10] "Phoebe: Ooh! Oh! (She starts to pluck at the air just in front of Ross.)"
## [11] "Ross: No, no don't! Stop cleansing my aura! No, just leave my aura alone, okay?"
## [12] "Phoebe: Fine! Be murky!"
假使原始文字檔非常龐大,我們可以加入參數 n 限定讀入的筆數。
## > file_path <- "https://s3-ap-northeast-1.amazonaws.com/r-essentials/friends_script.txt"
## > friends_script <- readLines(file_path, n = 2)
## > friends_script
## [1] "Ross: (mortified) Hi."
## [2] "Joey: This guy says hello, I wanna kill myself."