วันอังคารที่ 28 มกราคม พ.ศ. 2563

ไฟล์ใน R และการใช้งาน

 Files

 Reading Files with Functions 
     Read.table() function
     Variations of read.table()
      Read.csv() function
      Scan()function
Importing & Reading Files 
      Excel Files
      Minitab Files
      SPSS File
      Built-in datasets



ไฟล์ใน R

ไฟล์ใน R ปกติแล้วมีนามสกุลไฟล์ของชื่อไฟล์  ตัวอย่างเช่น ‘datafile.csv’ ส่วนขยายหรือนามสกุล‘.csv’ แม้ว่า R ไม่ได้รับรู้ถึงส่วนขยายนี้ แต่ก็ช่วยให้ผู้ใช้รู้ถึงความแตกต่าง บางนามสกุลไฟล์โดยพื้นฐานที่ใช้ในการโปรแกรม R รวมทั้ง ‘. rda’, ‘.r’, ‘.txt’, and ‘.csv’.

ต่อไปคือการอธิบายพอสังเขปของชนิดไฟล์ที่ใช้ใน R ต่างๆ กัน
  • ไฟล์นามสกุล .RDA  เหล่านี้คือออฟเจคของR ที่ถูกบันทึกไว้  ซึ่งได้แก่ไฟล์แนบไว้ และไฟล์ที่โหลด  โดยใช้ นามสกุลไฟล์เป็น .rda หรือ .RData   ไฟล์ที่มีนามสกุล .RData และ .rda ก็เป็นไฟล์แบบเดียวกัน 
  • ไฟล์นามสกุล .R  ไฟล์ที่ถูกสร้างขึ้นภายในเอดิเตอร์ของ R โดยใช้ฟังก์ชัน dump ที่รวมเอาคำสั่ง R  บางไฟล์ Rอาจจะมีนามสกุลไฟล์ .q , .
  • ไฟลนามสกุล .TXT  – มีเท็กซ์ไฟล์ที่ถูกใช้เพื่อเก็บชุดข้อมูล R ใช้ฟังก์ชัน read.table() และฟังก์ชันwrite.table()  R ใช้ read.table() สำหรับนำเข้าข้อมูล (data input) และ อ่านข้อมูลจากเท็กซ์ไฟล์  แล้วมีการสร้างดาต้าเฟรม(data frame)โดยอัตโนมัติภายใน  ฟังก์ชัน write.table() ในอีกทางหนึ่งถูกใช้ในการสร้างเท็กซ์ไฟล์ 
  •  ไฟล์นามสกุล .CSV – CSV หรือ Comma Separated Values  หรือใช้คอมม่าในการแยกค่าในไฟล์เป็นไฟล์ข้อมูลใช้กันทั่วไปแบบการนำเข้าข้อมูลโดยใช้ฟังก์ชัน read.csv()

ไฟล์แบบ .R ปกติแล้วเก็บข้อมูลปริมาณมาก หรือเป็นออฟเจ็คข้อมูลแทนที่จะเป็นแบบนำเข้าข้อมูลจากคีย์บอร์ด เราสามารถนำเข้าออฟเจคข้อมูลขนาดเล็กในคอนโซลของ R ได้ที่เครื่องหมายเตรียมพร้อมรับคำสั่ง แต่ดีที่สุดในการเก็บออฟเจอข้อมูลขนาดใหญ่ในไฟล์แบบ .rda, .r, .txt, .csv, และไฟล์ข้อมูลอื่นๆ  ที่สามารถนำมาใช้ได้ แล้วใช้ ฟังก์ชัน  read และ write ข้อมูลเข้าไปใน R

 การอ่านไฟล์ข้อมูลด้วยฟังก์ชัน (Reading files with Functions)

R อ่านออฟเจคข้อมูลขนาดใหญ่เป็นเหมือนค่ามาจากไฟล์ภายนอก  แม้ว่าสามารถนำเข้าข้อมูลเพียงจำนวนน้อยทางคีย์บอร์ดในการเขียนโปรแกรม R ครั้งหนึ่งๆ สิ่งแวดล้อมการโปรแกรมก็ไม่ยุ่งยาก และคงจะต้องใช้เวลานานมากในการนำเข้าข้อมูลปริมาณมาก ดังนั้นจึงดีกว่าที่จะขยายขอบเขตไฟล์นำเข้าข้อมูลโดยใช้ไฟล์เอดิเตอร์เพื่อให้สะดวกตามความต้องการของภาษา

ตัวแปรและชนิดอื่นของข้อมูลควรที่จะเก็บในดาต้าเฟรม และแล้วอ่านด้วยฟังก์ชัน read.table() หรือด้วยฟังก์ชันนำเข้าที่เก่ากว่าเรียกว่า scan()  ฟังก์ชัน read.table() ใช้กันทั่วไปในการอ่านข้อมูลในรูปของตารางแบบสี่เหลี่ยมผืนผ้า  แต่จะแนะนำให้ใช้ฟังก์ชัน scan() ในการอ่านข้อมูลเชิงตัวเลขเมทริกซ์ขนาดใหญ่มาก

ในตอนต่อไปนี้จะได้เรียนรู้มากขึ้นเกี่ยวกับ ฟังก์ชัน read.table() และวิธีการอย่างไรในการใช้ด้วยอาร์กูเมนต์ที่แตกต่างกันในการอ่านไฟล์ข้อมูลชนิดต่างๆกัน และยังมองหาวิธีการใช้อย่างไรของฟังก์ชัน scan() และ read csv() หากต้องการ

ฟังก์ชัน read.table()
ฟังก์ชัน read.table() ใช้ในการอ่านข้อมูลจากไฟล์ภายนอกเข้ามาใน R โดยสนับสนุนการใช้อาร์กูเมนต์หลายอย่าง  แต่ต้องการเพียงชื่อไฟล์เพื่ออ่านข้อมูลเข้ามาใน ฑ  โดยสามารถอ่านข้อมูลทั้งหมดจากไฟล์ภายนอกหนึ่งหรือสามารถระบุว่าชอบที่จะอ่านข้อมูลโดยใช้อาร์กูเมนต์อย่างไร  การอ่านไฟล์จากภายนอก จำเป็นต้องแน่ใจข้อมูลถูกนำมาแสดงในรูปแบบเฉพาะ  ในบรรทัดแรกของข้อมูลจะต้องมีชื่อของตัวแปรแต่ละตัวในแถว ด้านล่างแต่ละตัวแปร ควรแสดงค่างของแต่ละตัวแปร ถ้าค่าของตัวแปรนั้นละไว้  R จะคิดว่าเป็นความตั้งใจให้เป็นเช่นนั้น  ค่าตัวเลขในไฟล์ข้อมูลถูกอ่านเป็นตัวแปรจำนวน และไม่ใช่ตัวแปรจำนวน ทั้งนี้และทั้งนั้นยังสามารถที่จะเปลี่ยนได้ถ้าจำเป็น

ต่อไปเป็นตัวอย่างของไฟล์ข้อมูล โดยเรียกว่า "Inventory.data" ไฟล์นี้รวมเอาคอร์ลัมน์ ประกอบเป็นแถว ป้ายกำกับว่าตัวอย่าง 01  ไม่จำเป็นที่จะต้องมีแถวมีป้ายกำกับเป็นคอร์ลัมน์  แต่ทำได้โดยยอมให้ R ใช้แถวดีฟอลท์หรือปริยาย

ข้อมูลตามชื่อตัวแปร และแถวที่มีแถวที่ได้กำกับไว้ อาจคล้ายกับตัวอย่าง ต่อไปนี้                                                 
                   Price               Item                Code            Status               <--  นี้คือชื่อตัวแปร
01              20.00               Book               010A             Sold
02              1.00                 Pen                  9290              Order
03              .50                   Eraser              30Q1             Pending

เพื่อที่จะอ่านเนื้อหาทั้งหมดของไฟล์ข้อมูล “Inventory.data”  เราคงใช้ฟังก์ชัน read.table() เพื่ออ่าน data frame โดยตรงเข้าไปใน R โดยการใช้ประโยคคำสั่งต่อไปนี้
> InventoryStatus <- read.table(“Inventory.data”)

ถ้าเราเลือกที่จะละแถวที่มีคอลัมน์กำกับไว้จากไฟล์ข้อมูล เราคงจำเป็นต้องรวมเอาหัวของอาร์กูเมนต์ เมื่ออ่านข้อมูล นีเป็นเรื่องที่เราจะอ่านข้อมูลเข้ามาอย่างไรด้วยอาร์กูเมนต์หัวรายการ(header) และตัวที่ใช้แยกข้อมูล
 > InventoryStatus <- read.table(“Inventory.data”, header=TRUE, sep=””)

ทั้งประโยคคำสั่งที่กำหนดให้กับเนื้อหาของไฟล์ไปยังออฟเจคหนึ่ง  ข้อมูลที่อยู่ใน “ Inventory.data” ไฟล์ถูกกำหนดให้ “InventoryStatus” object. file is assigned to the “InventoryStatus” object.

จากนี้คือคำอธิบายของประโยคคำสั่ง InventoryStatus – นี้คือออฟเจคที่จะเก็นเนื้อหาของ “Inventory.data”.
read.table() – ฟังก์ชันread.table() ต้องการไฟล์ภายนอก ในกรณีนี้บรรจุไว้ด้วยไฟล์ “Inventory.data”

file. header=TRUE – นำมาใช้ถ้าไฟล์ไม่ได้บรรจุป้ายกำกับแถวใดๆ ไว้  แต่มีชื่อของตัวแปรในบรรทัดแรก  ตัวแปรดังกล่าวบ่งถึงไฟล์ที่มีหัวรายการ  เมื่อไฟล์ไม่มีคอร์ลัมน์สำหรับป้ายกำกับแถว แล้วป้ายกำกับแถวถูกกำหนดด้วย
   sep=”” – นี่คือตัวแยกฟิลด์ใช้สำหรับแยกไฟล์ออกจากไฟล์  คู่่ของเครื่องหมายคำพูดแทน whitespaces และสามารถมีช่องว่างหนึ่งหรือมากกว่าหนึ่งช่องว่าง, บรรทัด, แท็บ(tabs) หรือการเคาะคีย์รีเทิร์น  เราสามารถใช้แนวทางต่อไปนี้คือ

Space delimited data – ให้ใช้ sep=” “ สำหรับข้อมูลจำกัดช่องว่าง (space delimited data)
Tab delimited data –  ให้ใช้ sep=”\t”  สำหรับข้อมูลจำกัดแท็บ
New line delimited data – ให้ใช้“sep=”\n” สำหรับข้อมูลจำกัดบรรทัดใหม่(new line delimited data)
หมายเหตุ  ข้อมูลหรือเท็กซ์ที่ทำให้จำกัดคือรูปแบบไฟล์อย่างง่าย ที่แยกห่างกันด้วยอักขระที่ตายตัว  โดยเท็กช์ปกติแล้วเป็นแต่ละส่วน และอักขระที่ใช้จะเรียกว่าตัวจำกัด(delimiters)s.

Variations of read.table()   
ฟังก์ชันread.table() นำไปใช้ในหลายรูปแบบในการอ่านข้อมูลเข้ามาใน R จากกริดรูปสี่เหลี่ยมผืนผ้า.  อาร์กูเมนต์ต่างๆ ที่ใช้ภายในฟังก์ชันเพื่อจัดการชนิดต่างๆของข้อมูลที่แตกต่างกัน  ต่อไปนี้เป็นรายการของการใช้ฟังก์ชัน theread.table() ต่างๆ กัน
1.  Encoding – เข้ารหัสเมื่อไฟล์หนึ่งบรรจุไว้ด้วยตัวอักขระที่ไม่ใช่ ASCII  เราควรแน่ใจได้ว่าการอ่านได้อย่างถูกต้อง ถ้าเราอ่านไฟล์ Latin-1 ในตำแหน่ง UTF-8 เราคงจำเป็นต้องใช้ประโยคคำสั่งต่อไปนี้ โดยทำงานสำหรับสตริงLatin-1 แต่อาจจะทำงานไม่ได้สำหรับChinese/Russian/Greek และบริเวณที่คล้ายคลึง อาร์กูเมนต์สำหรับไฟล์เข้ารหัส ถูกใช้ในการกำหนดชนิดของไฟล์เหล่านี้ read.table(“Inventory.data", fileEncoding="latin1")
 2.  Header – อาร์กูเมนต์หัวรายการควรจะใช้เมื่อไฟล์นั้นมีเพียงชื่อตัวแปร เมื่อR รู้ว่าไม่มีป้ายกำกับแถว แล้วจะเซ็ต header = TRUE ถ้าเรามีไฟล์หนึ่งที่ไม่มีป้ายกำกับแถว เราสามารถอ่านข้อมูลด้วยสิ่งที่คล้ายคลึงกับประโยคคำสั่งต่อไปนี้ read.table(“Inventory.data”, header=TRUE, row.names=1)

3.    Separator – The default separators, sep =”” and sep =”\t”, is used to create white spaces in files. This is used to create spaces, tabs, or newlines. The type of separator used will determine the input based on the quote argument. The following statement uses the new line separator. This means the when the data is read into R, each value will be placed on a new line. InventoryStatus <- read.table(“Inventory.data”, header=TRUE, sep=”/n”)

4.    ค่าที่ขาดไป (Missing Values) – คิดได้ว่าโดยปริยาย สำหรับไฟล์ที่มีค่าที่ขาดหาย และใช้ ์์ ์NA เพื่อแทนค่าที่ขาดหายเหล่านั้น  ถ้ามีค่าจำนวนที่ขาดหายในคอร์ลัมน์   NaN, Inf, และ –สตริงอักขระ Inf นำออกมาแสดง  ในฟิลด์ที่ว่า หรือ ค่าที่ขาดหายอาจแสดงเหมือนดังต่อไปนี้
                   Price                        Item                         Code                             Status           

01              20.00                         Book                       010A                            Sold
02              1.00                           Pen                          NaN                             NA
03              .50                            Eraser                       30Q1                           Pending

5.    Unfilled lines – When a file is exported from a spreadsheet, there might be some empty fields with missing separators. To read these files, use set fill = TRUE. InventoryStatus <- read.table(“Inventory.data”, header=TRUE, fill=TRUE)

6.    White spaces – If there is a separator, leading, or a trailing whites spaces in a character field, you can strip the space with the strip.white = TRUE argument. InventoryStatus <- read.table(“Inventory.data”, header=TRUE, strip.white=TRUE)

7.    Blank lines – The read.table() function ignores empty lines by default, but you can change it by declaring blank.lines.skip=FALSE. This only works with fill=TRUE. InventoryStatus <- read.table(“Inventory.data”, header=TRUE, fill=TRUE, blank.lines.skip=FALSE)
 8.    Classes – Theread.table()by default reads the columns as character vectors and then choose a class for each variable in the file. It checks for integers, logical numbers, numeric values, and complex variables. If there are missing entries, then the variables are converted to a factor. The colClasses used in the following statement to convert the character vectors to factors in the “Inventory.data” file.
read.table("Inventory.data", header = TRUE, colClasses = classes)

9.    Comments – Theread.table() function by default uses the ‘#’ to create comments. When R sees the ‘#’ in a quoted string, it ignores the rest of the line. If there are white spaces with a comment, the comment is treated as a blank line. When there are no comments in the file, the comment.char=”” syntax is used like how it is used in the following statement.

read.table(“Inventory.data”, comment.char="#")

10. Escape and Backslash – Operating systems have different ways of using the backslash and escape keys in text files. However, Windows uses the backslash in path names. In R, the backslash is optional in data files.Whenread.table() uses allowEscapes, which is false by default, the backslashes are then interpreted as escapes quotes. Escape arguments are interpreted with control characters, such as \a, \b, \f, \n, and \r.

11. Convenience functions– Theread.csv()andread.delim()functions provide arguments to theread.table()function for tab delimited and CSV files that are exported from spreadsheets in English speaking locations. Read.csv() function Theread.csv() function is used to read a data file with the “comma separated values” (csv) format. CSV files contain values with numbers and letters separated with a comma. The first row of the file usually contains label names for differentiating the columns for the values. Here is an example of a CSV with some data. It contains three columns and three rows of data. The data file is called “products.csv”. The columns are labeled “Product”, “Price”, and “Quantity”. It is assumed that the rows have three products, “Book”, “Stationary”, and, “Toy”. Here is how the data file might look with the data. Product              Price                            Quantity
Book                            9.99                            2
Stationary              3.00                            6
Toy                            24.00                            3

To read the data file, you will need to use the “read.csv” command. The command must have at least one argument, but you can have three or more arguments. In the following example, the first argument is the name of the data file, the second argument the first row for the labels, and the third argument specifies that there is a comma between each line. Here is how you can read the data and assign it to the “inventoryinfo” variable with three arguments. > inventoryinfo <- read.csv(file="products.csv",head=TRUE,sep=",")

The variable “inventoryinfo” contains three columns with data. A name is assigned to each column. The column names are on the first line within the file. To access each column, use the “$” to differentiate the names of the columns. For example, if you would like to access the data in the “Price” column, you would use the following command: > inventoryinfo$Price

If you are not sure which column in the data file you would like to access use “names” command to list all the names of the columns in the data file. Here is an example of how you would use the “names” command: > names(inventoryinfo)

คำสั่ง “summary” สามารถนำมาใช้เพื่อเรียกข้อมูลจากไฟล์ข้อมูล CSV   เป็นการใช้หลังจากไฟล์ข้อมูลถูกกำหนดให้ตัวแปร  จากนี้จะเป็นการใช้คำสั้่ง‘summary’ อย่างไร.
> summary(inventoryinfo)

ถ้าไฟล์นั้นวางอยู่ในไดเรคตอรีเฉพาะ เราคงจำเป็นต้องระบุไดเรคทอรีและชื่อโฟนเดอร์ เพื่ออ่านไฟล์ ข้อตกลงในการตั้งชื่อสำหรับการอ่านไฟล์จากโฟนเดอร์และไดเรคเทอร์หาได้จากโฟนเดอร์ข้อความช่วยเหลือ เพื่อสำรวจดูรายการของทางเลือกสำหรับอ่านไฟล์ ให้ใช้คำสั่ง help(read.csv)

หมายเหตุ:  ถ้าเราใช้ระบบปฏิบัติการวินโดว์ เราคงจำเป็นต้องใช้เบคสแลซ 2 ครั้ง(“\\”) ในการจัดการไฟล์นั้น

ถ้าเราไม่สามารถหาไฟล์นั้นได้ เราอาจมองหาไปที่โฟลเดอร์หรือไดเรคทอรีที่ผิดพลาด ให้ใช้คำสั่ง dir() เพื่อระบุที่อยู่ไฟล์พร้อมกับแยกแยะไฟล์ ถ้าเราลืมซื่อไฟล์  คำสั่ง getwd() จะแสดงไดเรคทอรีที่เราทำงานอยู่ล่าสุดให้เห็น

ถ้าเรามีข้อมูลอยู่ในรูปสเปรดชีท เราสามารถแปลงไฟล์นั้นเป็นไฟล์CSV  โดยการขยัดแถวบนสุดของแถวในไฟล์สเปรดชีท และบันทึกไฟล์เป็นไฟล์“.csv”   เอ็กเซลยอมให้เราบันทึกสเปรดชีทเป็นไฟล็ CSV ได้   เพื่อให้เข้าใจได้ดีขึ้นว่า R เก็บข้อมูลได้อย่างไร ให้เปิดไฟล์ CSV ในเอ็กเซล  หลังจากที่เราแปลงสเปรดชีทเป็นไฟล์ CSVแล้ว เราสามารถอ่านข้อมูลไฟล์นี้ไปเป็นตัวแปรหนึ่งตามปกติ
ตัวอย่างเช่น  ถ้าเราแปลงไฟล์เอ็กเซลไฟล์หนึ่งเรียกว่า “datainventory.xls” ไปเป็น “datainventory.csv”, เราสามารถอ่านเข้าไปในตัวแปร “data” โดยใช้คำสั่งต่อไปนี้

 > data <- read.csv(file="datainventory.csv",header=TRUE,sep=",");

ตัวอย่างนี้สร้างตัวแปรใหม่่ชื่อ “data”. เมื่อเรานำเข้าข้อมูล “data” ที่เครื่องหมายเตรียมพร้อม  ข้อมูลทั้งหมดที่เก็บไว้ในตัวแปรจะแสดงให้เห็น
หมายเหตุ: R เก็บเส้นทางข้อมูบในหลายทางเมื่อเราใช้คำสั่ง “read.csv”  โดยการใช้ตัวแปร “data frame” ชนิดตัวแปรที่เก็บข้อมูลทั้งหมดในรูปแบบของ data frame format ด้วยการแยกคอร์ลัมน์  เพื่อตรวจสอบชนิดของตัวแปรที่เรามี ให้ใช้คำสั่งattributes           
 ต่อไปเราคงเป็นวิธีใช้คำสั่งอย่างไรเพื่อหาข้อมูลชนิดตัวแปร “data”
ตัวอย่างเช่น  จากนี้จะแสดงรายการต่างๆ ที่ R อธิบายตัวแปร
 > attributes (data)

Scan() function
ฟังก์ชัน scan() นำไปใช้อ่านชนิดต่างๆของข้อมูล หรือ ออฟเจคข้อมูล  ตัวอย่างเช่น เวคเตอร์ข้อมูล เราสามารถปรับคำสั่งให้อ่านข้อมูลเฉพาะ คำสั่งที่รอสำหรับการนำเข้าจากผู้ใช้และคืนกลับค่านำเข้าที่เครื่องหมายเตรียมพร้อม  เพื่อที่จะอ่านเวคเตอร์ข้อมูล 3 เวคเตอร์จากไฟล์ข้อมูล “datavectors.dat”,  เราคงต้องใช้คำสั่งต่อไปนี้โดยใช้ตัวแปร “datav”
ตัวอย่างเช่น
    > datav <- scan(“datavectors.dat”, list(“”,1,1))

อาร์กูเมนต์ตัวที่สอง list(“”,0,0) คือdummyตัวหนึ่ง หรือโครงสร้างที่ล้มเหลว(false structure) สำหรับสร้างเว็คเตอร์ 3 เวคเตอร์ที่จะถูกอ่านเข้ามาในตัวแปร “datav”  ส่วนโดยใช้คำสั่งต่อไปนี้:
    > datalabel <- datav[[1]]; x <- datav[[2]]; y <- datav[[3]]

ให้สังเกตว่าเวคเตอร์ทั้งสามถูกเก็บไว้ในตัวแปร “datalabel” ซึ่งจะถูกใช้ในการอ่านเข้ามาเป็นเวคเตอร์  นี่คือการที่จะเข้าไปจัดการเวคเตอร์อย่างไรด้วยฟังก์ชัน scan()
   > datav <- scan("datavectors.dat", list(id="", x=1, y=1))

เพื่อเข้าจัดการตัวแปรโดยแยกจากกัน, ให้ใช้เครื่องหมายหรือสัญลักษณ์ “$”
    > datalabel <- datav$id; x <- datav$x; y <- datav$y

Importing & Reading Files
There are various file formats that can be imported into R. Earlier we discussed reading CSV and XLS files into R. Here we will revisit these files, as well as other file formats. We will discuss importing file formats such as Minitab, SPSS, and TXT files. Excel Files Most of the time the data that you would like to import is in Excel format. Since most organizations use Excel files, you will need to understand how to use various methods for importing Excel data in R. These methods will import Excel data into R before you start using it. They have their advantages and disadvantages, but they all read data from an Excel spreadsheet and return the data in a data frame into R. Here is an overview of the some of the methods used to read data from an Excel spreadsheet into R. Save Excel files as Text – You can save Excel files into CSV format in an Excel spreadsheet or with an external tool that allow batch processing. You can then use the “read.table()” function to import the text format (that is CSV). > txt <- read.table(“textfile.csv”, header = TRUE) Copy and Paste Data from Excel– This is one of the easiest solution or method, but you will need to open the Excel file, select the data, and then copy with the copy and paste commands or with the copy and paste options available in Excel. You can use this method when you would like to get things done quickly. This requires the “read.table()” function. > txt <- read.table(“clipboard”) Excel ODBC driver– This method requires the installation of the Excel Open Database Connectivity (ODBC) driver. This method is not well recommended because it requires several lines of code to install the drive, connect with the Excel file, and read the file. This method is used with Microsoft Windows and 32-bit R. The following example shows how to establish a connection, get the data sheet, and close the connection to the Excel file. First, open a connection to the Excel file. > require(RODBC) > connFile <- odbcConnectExcel(“excelFile.xlsx”) #open a connection Second, display and read the data sheet. > sqlTables(connFile) $tableName, “Sheet1”) > txt <- sqlFetch(connFile,”Sheet1”) # display all data sheets > txt <- sqlQuery(connFile, “select * from [Sheet1 $]”) # read data sheet Third, close the connection to the file. > close(connFile) # close the connection gdata package The gdata package is a cross platform solution that works on Windows, Mac OS, and Linux operating systems. The gdata package requires that you install Perl libraries, which should already be available on Linux and Max systems. However, it may require additional steps for Windows platforms. > require(gdata) > txt <- read.xls(“excelFile.xlsx”, datasheet = 1, header = TRUE) xlsReadWrite package The xlsReadWrite package does not support xlsx files and it is not widely use these days, but it is a method that can used. It requires the use of third party code from GitHub and CRAN and only works on Windows platforms. > require(xlsReadWrite) > xls.getshlib() > txt <- read.xls(“excelFile.xlsx”, datasheet = 1) XLConnect package The XLConnect package that is a Java based solution that works across most platforms. This works best with small data sets. If you are working with large data sets, it may take a long time. > require(XLConnect) > xlsFile <- loadWorkbook(“excelFile.xlsx”) > txt <- readWroksheet(xlsFile, datasheet = “Sheet1”, header = TRUE) It is preferable to import Excel files as CSV files using the “read.table()” function mentioned in (1) and in the “Reading Files with Functions”section. It is the easiest way to import xlsx files into R. However, if you have small data sets, you can use the XLConnect method. Minitab Files Minitab files contain data stored as the Minitab Portable Worksheet format. It uses the “read.mtp” file to read the xlsx file. The file returns a list with one component for each column, matrix, or constant that is stored in the Minitab worksheet. The process requires that you first load the “foreign” package, call the “read.mtp” function, and then read in the “.mtp” file. First, load the “foreign” package. > library(foreign) Second, call the “read.mtp” file. > help(read.mtp) Third, read the “.mtp” file. > myfile <- read.mtp(“myfile.mtp”) SPSS File An SPSS file or Statistical Package for the Social Sciences is a file format used to store data for statistical analysis. The file is stored with the “.sav” extension. SPSS files are data files that are opened with the “read.spps” function from the “foreign” package. The “to.data.frame” argument is used inside the function to determine if a data frame will be returned. The following process shows how to load, access, and read spss files. First, load the “foreign” package. > library(foreign) Second, access the “read.spss function. > help(read.spss) Third, read the “.sav” file. myfile <- read.spss(“dataFile.sav”, to.data.frame=TRUE)

Built-in datasets 
มี ชุดข้อมูล(datasets)มากกว่า 100 ชุดตามรายการใน แพคเกจ“datasets”   เพื่อเข้าไปดูรายการชุดข้อมูล ให้ใช้คำสั่ง data()” ที่เครื่องหมายเตรียมพร้อม  ก็จะแสดงรายการชุดข้อมูลตามชื่อตามลำดับตัวอักษร พร้อมกันนั้นยังโหลดชุดข้อมูลเข้าไปในหน่วยความจำสำหรับการวิเคราะห์ทางสถิติ
ตัวอย่างเช่น ถ้าเราต้องการโหลดชุดข้อมูล “infert” เข้าสู่หน่วยความจำ  เราควรนำเข้าที่เตรียมพร้อมรับคำสั่งดังนี้
 > data(infert)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

โครงสร้างควบคุม ใน R

โครงสร้างควบคุม (Control Structures) โครงสร้างควบคุมใช้เพื่อควบคุมการไหลของประโยคคำสั่งที่ถูกสั่งให้ทำงานโดยตัวแปลของ R  โดยรวมประโยคคำสั...