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)
nbadata <- read.csv('http://datasets.flowingdata.com/ppg2008.csv')
nbadata

No comments:
Post a Comment