Tuesday, July 28, 2015

R programming - Data frames

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 


Whats a data frame?
A data frame is like a spread sheet. We can create multiple vectors and make a spread sheet by using the following:


x<-data.frame(vector1,vector2,vector3)

Example:
Create some vectors.

id<-1:5
firstname<-c("bob","harry","rosy","alice","hilary")
lastname<-c("bob1","harry1","rosy1","alice1","hilary1")
age<-c(10,15,20,25,30)

Create a data frame
x<-data.frame(id,firstname,lastname,age)



As you can see data frames are nothing but a spread sheet.
We can import data from a data base into a data frame.

There are some functions that may help you while working with a data frame.

Function nrow
nrow(x) function will return the number of rows in a data frame.

Function ncol
ncol(x) function will return the number of columns in a data frame

Function dim
dim(x) will return the number of rows and columns (combining ncol and nrow)




To view first 6 rows from a large data frame, use head(x)
To view the last 6 rows from a large data frame, use tail(x)

To find a particular column name within a data set, use names(x)[3]
To see all columns names within a data set, use names(x)



Want to access a particular value within a data frame?

Use x[row_no,col_no]

Thus, x[2,3] will return the 2nd row and the 3rd column data.



If you want to see the entire row, leave that blank

x[,3] will return the entire 3rd column values



Its pretty obvious that if you want to see the entire 2nd row, you need to use
x[2,]




If you want a part of the columns (and not all)

Use x[2,1:3] to return the 2nd row and only the first 3 column data values




Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 

No comments:

Post a Comment