Why my R function is showing me Length class mode instead of frequencies?

Viewed 2275

I am currently taking a course in statistics, and they asked us to install R and R environment to use during the course. This my first time using R. Our first task is to test some csv files using these commands:

ex.1 <- read.csv('ex1.csv')
summary(ex.1)
colnames(ex.1)

The results should look like this:

       id                          sex                      height     
Min.   :1538611                 FEMALE:54               Min.   :117.0  
1st Qu.:3339583                 MALE  :46               1st Qu.:158.0  
Median :5105620                                         Median :171.0  
Mean   :5412367                                         Mean   :170.1  
3rd Qu.:7622236                                         3rd Qu.:180.2  
Max.   :9878130                                         Max.   :208.0  

However, I am getting this (I included my entire code):

> getwd()
[1] "C:/Users/hp/Documents"
> setwd("C:/Users/hp/Documents/R")
> dir()
[1] "ex1.csv"     "ex2.csv"     "flowers.csv" "pop1.csv"    "pop2.csv"   
[6] "pop3.csv"    "win-library"
> ex.1 <- read.csv('ex1.csv')
> summary(ex.1)
       id              sex                height     
 Min.   :1538611   Length:100         Min.   :117.0  
 1st Qu.:3339583   Class :character   1st Qu.:158.0  
 Median :5105620   Mode  :character   Median :171.0  
 Mean   :5412367                      Mean   :170.1  
 3rd Qu.:7622236                      3rd Qu.:180.2  
 Max.   :9878130                      Max.   :208.0  
> colnames(ex.1)
[1] "id"     "sex"    "height"

What is the problem?

1 Answers

Do this ex.1$sex<- as.factor(ex.1$sex) . Then try summary command

The thing is when you are reading it as csv it reads sex column as characters. You have to make it as factor.

Related