Tuesday, August 8, 2017

Learn R Part 4 : Introduction to Data Frames


      Data Frame structure is like a data table or an excel spreadsheet. It has columns and each row in a column has particular data type. To create a simple data frame, we need to call data.frame() function and pass couple of vectors.

product <- c('Monitor', 'Mouse', 'Keyboard', 'Laptop')
prices <- c(100,10,15,430)
instock <- c(T,T,T,F)
frame <- data.frame(product,prices,instock)
print(frame)


       Okay, we have structured data, now let's see how to access to the values of this set.To get the individual column of a data frame, we need to use double brackets.We can pass the index of the column or the name of the column to get the column's data.

product <- c('Monitor', 'Mouse', 'Keyboard', 'Laptop')
prices <- c(100,10,15,430)
instock <- c(T,T,T,F)
frame <- data.frame(product,prices,instock)
print(frame)
col1 <- frame[[2]]
print(col1)
col2 <- frame[['instock']]
print(col2)




      If you are not fan of all these double brackets, you can use the shorthand notation.Data Frame Name, dollar sign and the column name gives you the column data too.Let's try to get the prices column's data again by using this method.

product <- c('Monitor', 'Mouse', 'Keyboard', 'Laptop')
prices <- c(100,10,15,430)
instock <- c(T,T,T,F)
frame <- data.frame(product,prices,instock)
print(frame)
col1 <- frame$prices
print(col1)




     Now, I think we are good place to read data from a data source and create a data frame. This is a big step because it's the first step to load real world data to analyze it. I am trying to load a public NBA dataset in CSV format to my data frame in the following example.

nbadata <- read.csv('http://datasets.flowingdata.com/ppg2008.csv')
nbadata


No comments:

Post a Comment