Skip to content

Econometrics with R – Part2


 

 

 

 

R นั้นเป็นโปรแกรมที่เหมาะที่จะวิเคราะห์ข้อมูล การป้อนข้อมูลเข้าวิเคราะห์ใน R นั้นจึงนิยมทำในโปรแกรมอื่นก่อน เช่น ใน spreadsheet (MS Excel) หรือ SPSS แล้วค่อยนำเข้า (Import) ข้อมูลดังกล่าวเข้ามาวิเคราห์ใน R โดยสามารถทำได้โดยใช้ command ด้านล่างนี้

1. จากไฟล์นามสกุล .csv

สมมุติว่าต้องการนำเข้าไฟล์ data.csv
โดยแถวแรกของข้อมูลนั้นเป็นชื่อตัวแปร

data <- read.table("c:/data.csv",
header=TRUE,
sep=",",
row.names="id")

2. จาก MS Excel

ให้  export ไฟล์ออกมาให้อยู่ในรูป .csv แล้วทำตามข้อที่ 1.

3. จาก SPSS หรือ  PASW (IBM)

ให้  export ไฟล์ออกมาให้อยู่ในรูป .csv ก่อน
โดยใช้คำสั่ง “Save as” แล้วเลือกนามสกุลของไฟล์เป็น .csv
จากนั้นทำการนำเข้าข้อมูลไปในโปรแกรม R ตามข้อที่ 1

4. จาก Stata

library(foreign)
data <- read.dta("c:/data.dta")

ตัวอย่างการเลือกตัวแปรเพื่อสร้างฐานข้อมูลย่อย (sub_data) จากฐานข้อมูลหลัก (data)

โดยสามารถเลือกตัวแปรได้โดยการ คลิ๊ก ร่วมกับ Shift (เลือกช่วงตัวแปร) และ Ctrl (เลือกทีละตัว)

data <- data.frame(replicate(26,list(rnorm(5))))
names(data) <- LETTERS
sub_data <- data[select.list(names(data), multiple=TRUE)]

Source: Adapted from an answer of  Josh O’Brien in Stackoverflow to the question from  Jeromy Anglim

กลับสู่สารบัญ

Related posts

4 Comments Post a comment
  1. Pairach, this is great! I’m trying to pick up on R but the official documentation isn’t the most clear. Just a suggestion, you could add in a few links to data sources you recommend to analyze. Something free, and safe to download would be great.

    October 19, 2011
    • Many thanks Will. For the data sources, I have included the dataset use in this session “migration.xls” at the beginning of the post. Is this what you suggested?

      October 19, 2011
      • Breezed right over it. Thanks Pairach!

        October 21, 2011
    • I am also gathering some useful resources for new R useRs but still organising them. I wish I can share them in this website soon.

      October 19, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: