Tuesday, April 22, 2008

Learning Statistics using R : One Categorical Variable

In the course, second activity teaches how to summaries collected data. Course also teaches how to draw pie charts and other basic charts using Excel. We will do the same using R. The data set used for this activity can be downloaded from here. The data set consists of answers of 1200 US college students. The question that was asked "With whom do you find it easiest to make friends: opposite sex, same sex or no difference?"

In this activity we will use the collected data to:
  1. Learn how to use R to tally our data into a table of counts and percents.
  2. Learn how to use R to produce a pie chart and other charts.
  1. Learn how to use R to tally our data into table of counts and percents. We save "friend.xls" file as a friend.csv file. We can use simple "summary" command of R to get summary of our data set.
    # Bug-Fix: Gabriele Righetti from Italy.
    >friend <- read.csv("c://friends.csv", header=TRUE)
    # just print the content of our data set/frame.
    >print (friend) 
    # note default action is to print.
    # lets run summary command and see what is out put.
    # note following is the output of the command.          
    No difference:602   
    Opposite sex :434   
    Same sex     :164
    From the above out put we can see that data set contains what counts. In our example we can see that 162 students find is easy to make friends with same sex, while 434 are more comfortable with opposite sex when they have to make friends and 602 students find that sex does not make any difference when they make friends. Now lets use few R commands to do more.
    # we have data in friend object/data.frame.
    > table(friend)
    No difference   Opposite sex      Same sex
      602           434                   164
    As in above output R "table" command gives us something similar to the "summary" command but table command is more powerful and used for cross-classifying data. Lets us do pie and bar charts.
    These two simple commands draws pie char and bar chart. Look at the help pages of each to make them more interesting.

    Following are our pie and barcharts!

No comments: