0Pricing
R Academy · บทเรียน

การนำเข้าไฟล์ Excel ด้วย readxl

อ่านชีต .xls และ .xlsx พร้อมตัวเลือกการเลือกชีตและช่วงเซลล์

การนำเข้าไฟล์ Excel ด้วย readxl เป็นบทเรียน R Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน R Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส R Academy มีบทเรียนทั้งหมด 4 บทเรียน

เหตุใดจึงใช้ readxl กับไฟล์ Excel

ไฟล์ Excel (.xlsx และ .xls) พบได้ทั่วไปในแวดวงธุรกิจ แพ็กเกจ readxl อ่านไฟล์เหล่านี้ได้โดยไม่ต้องติดตั้ง Excel และไม่มีข้อกำหนดเกี่ยวกับ Java แพ็กเกจนี้ส่งคืน tibble และรองรับหลายชีต ช่วงที่มีชื่อ และการตรวจหาชนิดข้อมูลของเซลล์

library(readxl)

# readxl can read:
# .xlsx  - modern Excel format (XML-based)
# .xls   - older Excel format (binary)
# .xlsm  - Excel with macros (reads data, ignores macros)

# Core functions:
# read_excel()   - auto-detects .xls vs .xlsx
# read_xlsx()    - always reads as .xlsx
# read_xls()     - always reads as .xls
# excel_sheets() - lists all sheet names

cat('readxl requires no Java, no Excel installation!')

excel_sheets() — แสดงรายการชีตทั้งหมด

excel_sheets('file.xlsx') ส่งคืนเวกเตอร์อักขระที่มีชื่อชีตทั้งหมด ควรใช้ฟังก์ชันนี้ก่อนอ่านข้อมูลเพื่อตรวจสอบโครงสร้างสมุดงาน จากนั้นส่งชื่อชีตให้ read_excel()

library(readxl)

# Using readxl's built-in example file
path <- readxl_example('datasets.xlsx')

# List all sheets in the workbook
sheets <- excel_sheets(path)
print(sheets)

cat('\nNumber of sheets:', length(sheets))

read_excel() — การใช้งานพื้นฐาน

read_excel(path) จะอ่านชีตแรกโดยค่าเริ่มต้น และตรวจหาโดยอัตโนมัติว่าไฟล์เป็น .xls หรือ .xlsx ผลลัพธ์คือ tibble ที่เดาชนิดคอลัมน์จากข้อมูล

library(readxl)

path <- readxl_example('datasets.xlsx')

# Read the first sheet (default)
df <- read_excel(path)
print(head(df, 4))
cat('\nDimensions:', nrow(df), 'rows x', ncol(df), 'cols')

อาร์กิวเมนต์ sheet — เลือกตามชื่อหรือลำดับ

ใช้ sheet='Sheet1' เพื่อเลือกตามชื่อ หรือใช้ sheet=2 เพื่อเลือกตามตำแหน่ง (เริ่มนับดัชนีจาก 1) ทั้งสองวิธีใช้ได้ การใช้ชื่อมีความยืดหยุ่นกว่าหากลำดับชีตเปลี่ยน ส่วนการใช้ดัชนีสะดวกสำหรับการวนซ้ำ

library(readxl)

path <- readxl_example('datasets.xlsx')
sheets <- excel_sheets(path)
print(sheets)

# Read by name
df_name <- read_excel(path, sheet = 'iris')
cat('iris sheet rows:', nrow(df_name), '\n')

# Read by index
df_idx <- read_excel(path, sheet = 2)
cat('Sheet 2 rows:', nrow(df_idx))

range — การอ่านช่วงเซลล์

range='A1:D10' จะอ่านเฉพาะช่วงเซลล์ที่ระบุในรูปแบบสัญกรณ์ของ Excel วิธีนี้มีประโยชน์เมื่อเวิร์กชีตมีหลายตาราง มีเส้นขอบจากการจัดรูปแบบ หรือมีเมทาดาทาอยู่นอกพื้นที่ข้อมูลที่ต้องการ

library(readxl)

path <- readxl_example('datasets.xlsx')

# Read only the first 5 data rows of columns A-D
df <- read_excel(path, sheet='iris', range='A1:D6')
print(df)
cat('\nShape:', nrow(df), 'rows,', ncol(df), 'cols')

col_names — ชื่อคอลัมน์ที่กำหนดเอง

ตั้งค่า col_names=FALSE เพื่อข้ามแถวส่วนหัวและตั้งชื่อคอลัมน์โดยอัตโนมัติ ส่งเวกเตอร์อักขระให้ col_names เพื่อใช้ชื่อที่กำหนดเอง (และข้ามแถวส่วนหัว) ใช้ร่วมกับ skip ได้หากส่วนหัวอยู่คนละแถว

library(readxl)

path <- readxl_example('datasets.xlsx')

# Read with custom column names (skip original header)
df <- read_excel(
  path,
  sheet = 'iris',
  col_names = c('sepal_l','sepal_w','petal_l','petal_w','species'),
  skip = 1  # Skip the original header row
)

print(head(df, 3))
print(names(df))

อาร์กิวเมนต์ skip — ข้ามแถวเมทาดาทา

skip=n จะข้าม n แถวแรกของชีตก่อนเริ่มอ่านข้อมูล เหมาะสำหรับเวิร์กชีตที่มีชื่อรายงาน วันที่สร้าง หรือเมทาดาทาอื่น ๆ อยู่เหนือ​ตารางข้อมูลจริง

library(readxl)

# Simulating a sheet with metadata rows (using a range instead)
path <- readxl_example('datasets.xlsx')

# Skip first row (pretend it has a title)
# and read only first 5 data rows
df <- read_excel(
  path,
  sheet = 'chickwts',
  skip = 1,       # Skip first data row
  col_names = FALSE  # Row 2 has no header now
)

print(head(df, 4))

col_types — การระบุชนิดคอลัมน์

ใช้ col_types เพื่อแทนที่การเดาชนิดข้อมูล ชนิดที่ใช้ได้ ได้แก่ 'text', 'numeric', 'date', 'logical', 'skip' (ลบคอลัมน์นั้น) และ 'list' (สำหรับคอลัมน์ที่มีหลายชนิดข้อมูล)

library(readxl)

path <- readxl_example('datasets.xlsx')

# Read with explicit column types
# iris sheet: 4 numeric + 1 text
df <- read_excel(
  path,
  sheet = 'iris',
  col_types = c('numeric','numeric','numeric','numeric','text')
)

print(sapply(df, class))

การอ่านทุกชีตด้วย map()

ใช้ excel_sheets() ร่วมกับ purrr::map() เพื่ออ่านทุกชีตพร้อมกันเป็นรายการที่มีชื่อ องค์ประกอบแต่ละรายการจะเป็น tibble ของชีตนั้น ใช้ map_df(.id='sheet') เพื่อรวมแถวจากทุกชีต

library(readxl)
library(purrr)

path <- readxl_example('datasets.xlsx')
sheets <- excel_sheets(path)

# Read all sheets into a named list
all_data <- map(sheets, ~read_excel(path, sheet=.x))
names(all_data) <- sheets

# Show dimensions of each sheet
map_df(all_data, function(df) {
  data.frame(rows=nrow(df), cols=ncol(df))
}, .id='sheet')

n_max — จำกัดจำนวนแถวที่อ่าน

n_max=n จะอ่านข้อมูลไม่เกิน n แถว (ไม่รวมส่วนหัว) ใช้เพื่อดูตัวอย่างเวิร์กชีตขนาดใหญ่ โหลดตัวอย่างสำหรับการทดสอบ หรืออ่านข้อมูลเป็นส่วน ๆ ในสภาพแวดล้อมที่มีหน่วยความจำจำกัด

library(readxl)

path <- readxl_example('datasets.xlsx')

# Preview just the first 5 rows
preview <- read_excel(path, sheet='iris', n_max=5)
print(preview)
cat('\nPreviewed', nrow(preview), 'of', nrow(read_excel(path, sheet='iris')), 'rows')

อาร์กิวเมนต์ na — สตริงค่าที่หายไป

เช่นเดียวกับ readr แพ็กเกจ readxl รองรับอาร์กิวเมนต์ na ซึ่งระบุว่าสตริงข้อความใดควรถูกอ่านเป็น NA โดยทั่วไป Excel จะจัดเก็บค่าที่หายไปเป็นเซลล์ว่าง (ซึ่งระบบจัดการให้อัตโนมัติ) หรือเป็นสตริงตัวบ่งชี้ เช่น 'N/A' หรือ '#N/A'

library(readxl)

path <- readxl_example('datasets.xlsx')

# Handle Excel error strings and custom NA markers
# In real files these might be '#N/A', '#VALUE!', 'N/A', '-'
df <- read_excel(
  path,
  sheet = 'iris',
  na = c('', 'NA', 'N/A', '#N/A', '-')
)

cat('Missing values per column:\n')
print(colSums(is.na(df)))

ตรวจสอบความเข้าใจอย่างรวดเร็ว

excel_sheets('file.xlsx') ส่งคืนค่าอะไร

สรุป: การนำเข้าไฟล์ Excel

ประเด็นสำคัญสำหรับการนำเข้า Excel ด้วย readxl:

  • excel_sheets(path) — แสดงรายชื่อชีตทั้งหมด
  • read_excel(path) — อ่านชีตแรก และตรวจจับ .xls/.xlsx โดยอัตโนมัติ
  • sheet='Name' หรือ sheet=2 — เลือกชีตด้วยชื่อหรือลำดับ
  • range='A1:D10' — อ่านช่วงเซลล์ที่ระบุ
  • col_names, skip — จัดการแถวข้อมูลกำกับและส่วนหัวแบบกำหนดเอง
  • col_types = c('text','numeric','date','skip') — กำหนดชนิดข้อมูลแทนการเดาชนิดข้อมูล
  • n_max=5 — แสดงตัวอย่างไฟล์ขนาดใหญ่ และ na=c('N/A','#N/A') — กำหนดสตริง NA แบบกำหนดเอง
  • ใช้ร่วมกับ map(excel_sheets(path), ~read_excel(path, sheet=.x)) เพื่ออ่านทุกชีต
library(readxl)
library(purrr)

path <- readxl_example('datasets.xlsx')

# Full workflow: discover, select, read
cat('Sheets available:', paste(excel_sheets(path), collapse=', '), '\n\n')

# Read a specific sheet with explicit types
df <- read_excel(
  path,
  sheet = 'iris',
  col_types = c('numeric','numeric','numeric','numeric','text'),
  n_max = 5
)

print(df)

คำถามที่พบบ่อย

บทเรียน “การนำเข้าไฟล์ Excel ด้วย readxl” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การนำเข้าไฟล์ Excel ด้วย readxl” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส R Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส R Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การนำเข้าไฟล์ Excel ด้วย readxl”

อ่านชีต .xls และ .xlsx พร้อมตัวเลือกการเลือกชีตและช่วงเซลล์ คุณปฏิบัติ R Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน R Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน R Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การนำเข้าไฟล์ Excel ด้วย readxl” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน R Academy นี้ได้ไหม

ได้ บทเรียน R Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การอ่านไฟล์ CSV ด้วย read_csv()
  2. การแยกวิเคราะห์ TSV และไฟล์ความกว้างคงที่
  3. การนำเข้าไฟล์ Excel ด้วย readxl
  4. การเขียนข้อมูลในหลายรูปแบบ
← กลับไปที่ R Academy