R တွင် ဒေတာဘောင်ကော်လံများကို မည်သို့စုပုံမည်နည်း။


မကြာခဏဆိုသလို သင်သည် R တွင် ကော်လံနှစ်ခု သို့မဟုတ် ထို့ထက်ပိုသော ဒေတာဘောင်ကော်လံများကို စုစည်းလိုပေမည်။

ဥပမာအားဖြင့်၊ သင်သည် ဤအရာမှ စတင်လိုပေမည်။

 person trial outcome1 outcome2
     A 1 7 4
     A 2 6 4
     B 1 6 5
     B 2 5 5
     C 1 4 3
     C 2 4 2

အဲဒီအတွက်:

 person trial outcomes value
      A 1 outcome1 7
      A 2 outcome1 6
      B 1 outcome1 6
      B 2 outcome1 5
      C 1 outcome1 4
      C 2 outcome1 4
      A 1 outcome2 4
      A 2 outcome2 4
      B 1 outcome2 5
      B 2 outcome2 5
      C 1 outcome2 3
      C 2 outcome2 2

ဒါကိုလုပ်ဖို့ R မှာ သင်သုံးနိုင်တဲ့ နည်းလမ်းနှစ်ခုကို ဒီသင်ခန်းစာမှာ ရှင်းပြထားပါတယ်။

နည်းလမ်း 1- Base R ရှိ Stack လုပ်ဆောင်ချက်ကို အသုံးပြုပါ။

အောက်ဖော်ပြပါ ကုဒ်သည် Base R တွင် stack လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ကော်လံများကို မည်သို့ stack လုပ်ရမည်ကို ပြသသည်-

 #create original data frame
data <- data.frame(person=c('A', 'A', 'B', 'B', 'C', 'C'),
                   trial=c(1, 2, 1, 2, 1, 2),
                   outcome1=c(7, 6, 6, 5, 4, 4),
                   outcome2=c(4, 4, 5, 5, 3, 2))

#stack the third and fourth columns
cbind (data[1:2], stack (data[3:4]))

   person trial values ind
1 A 1 7 outcome1
2 A 2 6 outcome1
3 B 1 6 outcome1
4 B 2 5 outcome1
5 C 1 4 outcome1
6 C 2 4 outcome1
7 A 1 4 outcome2
8 A 2 4 outcome2
9 B 1 5 outcome2
10 B 2 5 outcome2
11 C 1 3 outcome2
12 C 2 2 outcome2

နည်းလမ်း 2- Reshape2 ၏ Melt Function ကိုသုံးပါ။

အောက်ပါကုဒ်သည် reshape2 ဒစ်ဂျစ်တိုက်ရှိ အရည်ပျော်သည့် လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ကော်လံများကို မည်သို့စုပုံရမည်ကို ပြသသည်-

 #loadlibrary
library(reshape2)

#create original data frame
data <- data.frame(person=c('A', 'A', 'B', 'B', 'C', 'C'),
                   trial=c(1, 2, 1, 2, 1, 2),
                   outcome1=c(7, 6, 6, 5, 4, 4),
                   outcome2=c(4, 4, 5, 5, 3, 2))

#melt columns of data frame
melt(data, id. var = c(' person ', ' trial '), variable. name = ' outcomes ')

   person trial outcomes value
1 A 1 outcome1 7
2 A 2 outcome1 6
3 B 1 outcome1 6
4 B 2 outcome1 5
5 C 1 outcome1 4
6 C 2 outcome1 4
7 A 1 outcome2 4
8 A 2 outcome2 4
9 B 1 outcome2 5
10 B 2 outcome2 5
11 C 1 outcome2 3
12 C 2 outcome2 2

ပေါင်းစည်းခြင်းလုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာတွေ့နိုင်ပါသည်။

ထပ်လောင်းအရင်းအမြစ်များ

R တွင် ကော်လံနှစ်ခုကို မည်သို့ပြောင်းရမည်နည်း။
R တွင် ကော်လံအမည်ပြောင်းနည်း
R တွင် သီးခြားကော်လံများကို ပေါင်းနည်း

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်