0% found this document useful (0 votes)
12 views13 pages

3 Scalar, Dataframe

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

3 Scalar, Dataframe

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SCALAR IN R

A scalar object is just a single value like a number or a name.


• Here are examples of numeric scalars: Numeric scalar
SCALAR IN R

• Scalars can also be characters (also known as strings). In R, you


denote characters using quotation marks. Here are examples of
character scalars:
• x = 1.5
• fname = 'Will’
• u = TRUE
• x = as.integer(4.8)
DATA FRAME

• DataFrames in R are generic data objects which are used to store the tabular data.
• Data frame is a two dimensional data structure in R.
• DataFrame is made up of three principal components, the data, rows, and columns.
• A data frame is a special type of list where every element of the list has same
length
• i.e. data frame is a “rectangular” list
• create a new data frame with data.frame() function.
• Find the number of rows and columns with nrow(dat) and ncol(dat), respectively.
• additional attribute: rownames()
• all columns in a data frame are of same type, data frame can be
converted to a matrix with data.matrix() or as.matrix().
• Also created by read.csv() and read.table()
• Data frame is a two dimensional data structure in R.
• A data frame is being used for storing data tables
• dat <- data.frame(id = letters[1:10], x = 1:10, y = 11:20)
• is.list(dat)
• class(dat)
• dat[1, 3]
• As data frames are also lists, it is possible to refer to columns using
the list notation, i.e. either double square brackets [[ ]]or a $.
• dat[["y"]]
• dat$y
USEFUL DATA FRAME FUNCTIONS

• head() - shows first 6 rows


• tail() - shows last 6 rows
• dim() - returns the dimensions of data frame (i.e. number of rows and number of
columns)
• nrow() - number of rows
• ncol() - number of columns
• str() - structure of data frame - name, type and preview of data in each column
• names() or colnames() - both show the names attribute for a data frame
• sapply(dataframe, class) - shows the class of each column in the data frame
HOW TO CREATE A DATA FRAME IN R?

• create a data frame using the data.frame() function

# Create the data frame.


emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15",
"2014-05-11", "2015-03-27"))
)

 emp.data
 summary(emp.data)
EXTRACT DATA FROM DATA FRAME

Extract specific column from a data frame using column name.

# Create the data frame.


emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-
05-11", "2015-03-27")) )

# Extract Specific columns.


result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)
EXTRACT DATA FROM DATA FRAME

# Extract first two rows.

result <- emp.data[1:2,]


print(result)

# Extract 3rd and 5th row with 2nd and 4th column.

result <- emp.data[c(3,5),c(2,4)]


print(result)
EXPAND DATA FRAME

Add column - Just add the column vector using a new


column name.

# Add the "dept" coulmn.


emp.data$dept <- c("IT","Operations","IT","HR","Finance")
v <- emp.data
print(v)
EXPAND DATA FRAME
# Create the second data frame
emp.newdata <- data.frame(
emp_id = c (6:8),
emp_name = c("Rasmi","Pranab","Tusar"),
salary = c(578.0,722.5,632.8),
start_date = as.Date(c("2013-05-21","2013-07-30","2014-06-17")),
dept = c("IT","Operations","Fianance")
)

# Bind the two data frames.


emp.finaldata <- rbind(emp.data,emp.newdata)
print(emp.finaldata)

You might also like