R Language –Data Structures : Online Course
R Data Frames
R Language –Data Structures : Online Course: So many programming language are available but R language is used for specific Statistical Computing and Data Analysis. Data Frame is in R . It is a two-dimensional data table. It is composed of three major components such as data, rows and columns. Statisticians, scientists, and programmers use r language in data analysis code.
Create a Data Frame
- A data frame is a type of data structure in R programming.
- It is a type of data table where data is stored in the form of rows and columns like a spreadsheet.
- For this reason, it is easily possible to import a CSV file or Excel spreadsheet into a data frame directly in R.
- The user can create multiple vectors, each vector holding a different type of data, and merge them all into a single data frame.
- All vectors passed to the frame() function must have the same length.
- As you know that matrix contains only one type of data, whereas a data frame accepts different data types (numeric, character, factor, etc.).
- Store each vector on a variable in the frame() function. By storing it on a variable, the columns will get the name of the variables.
Data Frame Syntax
variable = data.frame(df, stringsAsFactors = TRUE) |
- df : It is a collection of variable to join
- stringsAsFactors: Convert string to factor by default
Input: create student marks table using frame () |
# Create a data frame df = data.frame( id = c(1, 2, 3), name = c(‘UD’, ‘Rakesh’, ‘Ramesh’), marks = c(89.54, 52.14, 45.70)) # Print the data frame df |
Output: |
Named Data Frame
We can change column name in R with the function names().
Input: Using Name the data frame |
df = data.frame( id = c(1, 2, 3), name = c(‘Rahim’, ‘Rakesh’, ‘Ramesh’), marks = c(89.54, 52.14, 45.70), stringsAsFactors = TRUE) df # Name the data frame names(df) <- c(‘student_id’, ‘student_name’, ‘percentage’) df |
Output: |
Access Data Frame Elements
The components of the data frame are accessed via index numbers or the column names. The indexing of columns using a double square brace symbol [[ ]]
.
Column Name: We can also access the elements using the $ dollar symbol. Syntax behind this is: <DataFrame>$Column_Name
Input: Access Data Frame Elements using index numbers or the column names |
#Accessing Low level elements emp_Id <- c(1:6) Name <- c(“sumit”, “Amit”, “Nicky”,”Shweta”, “Priyanka”, “Rashmi”) Occupation <- c(“Syatem Administrator”, “Management”, “Developer”, “Programmer”, “Clerical”, “Admin”) Department <- c(“IT Dept”, “MGT”, “IT Dept”, “Coder”, “Clerical”,”MGT”) Salary <- c(80000, 90000, 75000, 92000, 68000, 82000) emp <- data.frame(emp_Id, Name, Occupation, Salary, Department) print(emp) #Accessing Element at 1st Row and 2nd Column print(“——————————————————–“) emp[1, 3] #Get Element at 4th Row and 3rd Column print(“————————–“) emp[4, 4] #Get All Elements at 5th Row print(“————————–“) emp[5, ] #Get All Item of the 4th Column print(“————————–“) emp[, 4] #Extract 3rd and 5th row with 2nd and 4th column. print(“————————–“) emp[c(1,3),c(2,5)] print(“————————–“) #Get all the Elements (Rows) Present in the Name Item (Column) emp$Name |
Output: |
Modifying Data Frame
In R, It give permission us to make modifications to our data frame. In this case we cannot only add rows and columns, but also we can delete them to the already existing data frame.
So we can use the cbind function for add column and rbind function for add row. Use the c()
function to remove rows and columns in a Data Frame.
Input: Modifying Data Frame for add and remove |
# Create the data frame. dt = data.frame( rollno = c(1:4), name = c(‘Rahim’, ‘Rakesh’, ‘Ramesh’, ‘Aman’), percentages = c(89.54, 52.14, 45.70, 60.45), stringsAsFactors = FALSE) dt #Using rbind function for Row addrow = data.frame(rollno=c(5),name=c(‘Mukesh’),percentages=c(91.45)) rbind(dt, addrow) #Using cbind function for column cbind(dt, age=c(21,20,21,22)) #Remove column dt$age = NULL dt #Remove Row dt = dt[-1,] dt |
Output: |
Remove Column & Row |
Summary of Data Frame
A statistical summary of the data is returned from the summary() function.
Input: Using summary () |
dt = data.frame( rollno = c(1:4), name = c(‘Rahim’, ‘Rakesh’, ‘Ramesh’, ‘Aman’), percentages = c(89.54, 52.14, 45.70, 60.45), stringsAsFactors = FALSE) summary(dt) |
Output: |
R Language: Online Course
आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।
Thanking You………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..
Read More
Reference