2012년 12월 22일 토요일

[R Study(Data Frame - No6 ]

It's been a long time since I posted. Actually I tried to make at least 2 new posts every week. I thought that was easy, however I think I am getting lazy.

Anyway, today I'll talk about "DATA Frame"
Data Frame is a columnar organization of data which is composed of named vectors, matrix or other data frame. (In fact, I don't mention other data objects that I didn't mention)



Let's make a  DATA Frame with different ways


> Student <- c("Minseo","DK","KyungHwa")
> Math_Score <- c(80,90,60)
> Eng_Score  <- c(80,60,70)
> Test_Result <- cbind(Student,Math_Score,Eng_Score)


[ Type A ]

> Test_Result_DF <- data.frame(Student, Math_Score, Eng_Score)
> Test_Result_DF
   Student Math_Score Eng_Score
1   Minseo           80        80
2       DK              90        60
3 KyungHwa         60        70

[ Type B ]


> Test_Result_DF2 = as.data.frame(Test_Result)
> Test_Result_DF2
   Student Math_Score Eng_Score
1   Minseo         80        80
2       DK         90        60
3 KyungHwa         60        70


As you can see, two different ways brought the same result.

Once you get a data frame, you can get a diverse results,  if you want to get a result that mathematics score is above 70


> Test_Result_DF[Math_Score>70,]
  Student Math_Score Eng_Score
1  Minseo         80        80
2      DK           90        60

Furthermore, you can  choose the column.

> Test_Result_DF[Math_Score>70,1]
[1] Minseo DK  
Levels: DK KyungHwa Minseo
> Test_Result_DF[Math_Score>70,c(1,2)]
  Student Math_Score
1  Minseo         80
2      DK           90
> Test_Result_DF[Math_Score>70,c(1,2,3)]
  Student Math_Score Eng_Score
1  Minseo         80        80
2      DK           90        60


I already said, data frame is a columnar organization of data which is composed of named vectors, matrix or other data frame. Therefore you can add data frame into another data frame.


> data.frame(Student, Math_Score2=Math_Score, Eng_Score2=Eng_Score)
   Student Math_Score2 Eng_Score2
1   Minseo          80         80
2       DK          90         60
3 KyungHwa          60         70
> Test_Result_DF3 <- data.frame(Student, Math_Score2=Math_Score, Eng_Score2=Eng_Score)
> merge(Test_Result_DF, Test_Result_DF3)
   Student Math_Score Eng_Score Math_Score2 Eng_Score2
1       DK         90        60          90         60
2 KyungHwa         60        70          60         70
3   Minseo         80        80          80         80
>




Data frame also gives useful functions.


  •  row control



> Test_Result_DF[1,]
  Student Math_Score Eng_Score
1  Minseo         80        80
> Test_Result_DF[2,]
  Student Math_Score Eng_Score
2      DK         90        60


  •  column control



> Test_Result_DF[2,2]
[1] 90
> Test_Result_DF[2,3]
[1] 60
> Test_Result_DF[,3]
[1] 80 60 70


  •  row count control



head (Test_Result_DF, 2)
  Student Math_Score Eng_Score
1  Minseo         80        80
2      DK           90        60


I think Data Frame is very useful.

See you next time.






댓글 없음:

댓글 쓰기