How to create a column (or should I say categorical variable)

r

#1

Hi,

While looking at mtcars dataset, the first column that is seen is mentioned as row.names which contains the car models. Why this column (or variable) is not counted when I look at dimensions of the dataset? Is this what is called a factor variable or categorical variable?


#2

row.names will not be counted in the dimension. If you want to include this as a variable, then do
mtcars$type <- rownames(mtcars)
This will create a new variable type.
to print the dataset without rownames, then do
rownames(mtcars) <- NULL


#3

Thanks. But while reading a data frame from a local system how do I create a such column which acts a row reference.


#4

@yaswanth912,
Can you please post the link of the mtcars dataset? Probably the row.names is the index in the data set that will not be considered as a variable, On the other hand a variable can be categorical and will still be counted in the dimension of data set.
regards,
Danish


#5

Well I am not sure how to post the link. below is the image how the dataset looks like. It is a builtin data set in base R under datasets package. From below image you can see that the variables start from mpg .


#6

@yaswanth912,
From the data you can see that the first column is index. Thats why it is not counted as variable and it has nothing to do with the variable being Categorical.
Regards,
Danish


#7

@syed.danish Thanks got it. How do I create a index while reading a dataset from local system? I mean if I am reading a data frame using read.table , how to give the index?


#8

@yaswanth912,
Please post the code that you used to take the input.


#9

@syed.danish Below is the code I used to read the data.
EmpSal <- read.table('Baltimore_City_Employee_Salaries_FY2015.csv', sep=',', header = TRUE, fill = TRUE, quote="\"")
This code reads the data and has variables as shown in below image. How can I set AgencyID as index here.


#10

@yaswanth912,
Did you try looking at the help for read.table in R using F1 key on your keyboard? That will help you to perform the desirable action.