R တွင် readlines() function ကိုအသုံးပြုနည်း (ဥပမာများဖြင့်)
R ရှိ readLines() လုပ်ဆောင်ချက်ကို ချိတ်ဆက်မှုအရာဝတ္ထုမှ စာသားမျဉ်းအားလုံး သို့မဟုတ် တစ်စိတ်တစ်ပိုင်းကို ဖတ်ရန် အသုံးပြုနိုင်သည်။
ဤလုပ်ဆောင်ချက်သည် အောက်ပါ syntax ကိုအသုံးပြုသည်-
readLines(con, n=-1L)
ရွှေ-
- အားနည်းချက်- ချိတ်ဆက်မှု အရာဝတ္ထု သို့မဟုတ် စာလုံး string တစ်ခု
- n- ဖတ်ရန် အများဆုံး စာကြောင်းများ။ မူရင်းမှာ စာကြောင်းအားလုံးကို ဖတ်ရန်ဖြစ်သည်။
အောက်ဖော်ပြပါနမူနာများသည် some_data.txt ဟုခေါ်သော အောက်ပါစာသားဖိုင်ဖြင့် ဤလုပ်ဆောင်ချက်ကို လက်တွေ့အသုံးပြုနည်းကို ပြသသည်
ဥပမာ 1- စာသားဖိုင်တစ်ခုမှ စာကြောင်းအားလုံးကို ဖတ်ရန် readLines() ကိုသုံးပါ။
စာသားဖိုင်ကို ကျွန်ုပ်၏ ကွန်ပြူတာရှိ ကျွန်ုပ်၏ Documents ဖိုဒါတွင် သိမ်းဆည်းထားသည် ဆိုကြပါစို့။
ဤစာသားဖိုင်မှ စာကြောင်းတစ်ခုစီကို ဖတ်ရန် အောက်ပါ readLines() လုပ်ဆောင်ချက်ကို ကျွန်ုပ်အသုံးပြုနိုင်သည်-
#read every line from some_data.txt
readLines("C:/Users/Bob/Documents/some_data.txt")
[1] “The first line of the file” “The second line of the file”
[3] “The third line of the file” “The fourth line of the file”
[5] "The fifth line of the file" "The sixth line of the file"
စာသားဖိုင်တွင် စာကြောင်း 6 ကြောင်းပါသောကြောင့် readLines() လုပ်ဆောင်ချက်သည် အရှည် 6 ဇာတ်ကောင်ကို ထုတ်လုပ်ပေးပါသည်။
လိုချင်ရင်၊ ဒေတာဘောင်ထဲမှာ စာသားဖိုင်ရဲ့ စာကြောင်းတွေကို သိမ်းထားနိုင်ပါတယ်-
#read every line from some_data.txt
my_data <- readLines("C:/Users/Bob/Documents/some_data.txt")
#create data frame
df = data. frame (values=my_data)
#view data frame
df
values
1 The first line of the file
2 The second line of the file
3 The third line of the file
4 The fourth line of the file
5 The fifth line of the file
6 The sixth line of the file
ရလဒ်မှာ ကော်လံတစ်ခုနှင့် အတန်းခြောက်တန်းပါသည့် ဒေတာဘောင်တစ်ခုဖြစ်သည်။
ဥပမာ 2- စာသားဖိုင်တစ်ခု၏ ပထမဆုံး N လိုင်းများကို ဖတ်ရန် readLines() ကိုသုံးပါ။
ကျွန်ုပ်၏ကွန်ပျူတာရှိ ကျွန်ုပ်၏ Documents ဖိုဒါတွင် စာသားဖိုင်ကို သိမ်းဆည်းထားကြောင်း ထပ်မံယူဆကြပါစို့။
ဤစာသားဖိုင်၏ ပထမဆုံး n လိုင်းများကိုသာ ဖတ်ရန် argument n ဖြင့် အောက်ပါ readLines() လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်ပါသည်။
#read first 4 lines from some_data.txt
readLines("C:/Users/Bob/Documents/some_data.txt", n= 4 )
[1] “The first line of the file” “The second line of the file”
[3] “The third line of the file” “The fourth line of the file”
readLines() function သည် length 4 ၏ character vector ကိုထုတ်ပေးသည်။
ဤစာသားဖိုင်ရှိ သတ်မှတ်ထားသော စာကြောင်းတစ်ကြောင်းသို့ သွားရန် စတုရန်းကွင်းကွင်းကိုလည်း အသုံးပြုနိုင်သည်။
ဥပမာအားဖြင့်၊ ကျွန်ုပ်သည် ဇာတ်ကောင် vector ၏ ဒုတိယစာကြောင်းကိုသာ ဝင်ရောက်ရန် အောက်ပါကုဒ်ကို သုံးနိုင်သည်-
#read first 4 lines from some_data.txt
my_data <- readLines("C:/Users/Bob/Documents/some_data.txt", n= 4 )
#display second line only
my_data[2]
[1] "The second line of the file"
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများတွင် အခြားဖိုင်အမျိုးအစားများကို R သို့ မည်သို့တင်သွင်းရမည်ကို ရှင်းပြထားပါသည်။
R တွင် read.table ကိုအသုံးပြုနည်း
CSV ဖိုင်များကို R သို့မည်သို့ထည့်သွင်းနည်း
Excel ဖိုင်များကို R သို့ဘယ်လိုတင်သွင်းမလဲ။