w3resource

R Programming: Create and Summarize employee Dataframes

R Programming: Basic Exercise-26 with Solution

Write a R program to create a Dataframes which contain details of 5 employees and display summary of the data.

Sample Solution :

R Programming Code :

# Create a data frame named 'Employees' with details of 5 employees
Employees = data.frame(
  # Employee names
  Name = c("Anastasia S", "Dima R", "Katherine S", "JAMES A", "LAURA MARTIN"),
  # Employee genders
  Gender = c("M", "M", "F", "F", "M"),
  # Employee ages
  Age = c(23, 22, 25, 26, 32),
  # Employee designations
  Designation = c("Clerk", "Manager", "Exective", "CEO", "ASSISTANT"),
  # Employee Social Security Numbers (SSN)
  SSN = c("123-34-2346", "123-44-779", "556-24-433", "123-98-987", "679-77-576")
)

# Print a message indicating the summary of the data
print("Summary of the data:")

# Print a summary of the 'Employees' data frame, which includes basic statistics
print(summary(Employees))

Output:

[1] "Summary of the data:"
     Name              Gender               Age       Designation       
 Length:5           Length:5           Min.   :22.0   Length:5          
 Class :character   Class :character   1st Qu.:23.0   Class :character  
 Mode  :character   Mode  :character   Median :25.0   Mode  :character  
                                       Mean   :25.6                     
                                       3rd Qu.:26.0                     
                                       Max.   :32.0                     
     SSN           
 Length:5          
 Class :character  
 Mode  :character                                     

Explanation:

  • Create a Dataframe:
    • Employees = data.frame(...) initializes a dataframe named Employees.
    • Name=c("Anastasia S","Dima R","Katherine S", "JAMES A","LAURA MARTIN") specifies the names of the employees.
    • Gender=c("M","M","F","F","M") specifies the gender for each employee.
    • Age=c(23,22,25,26,32) lists the ages of the employees.
    • Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT") includes the job titles of the employees.
    • SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576") provides the Social Security Numbers.
  • Print Summary:
    • print("Summary of the data:") prints a message indicating the upcoming summary.
    • print(summary(Employees)) displays a summary of the dataframe, including:
      • Count and distribution of values for each column.
      • Basic statistics for numeric columns (e.g., Age).

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to create a Data Frames which contain details of 5 employees and display the details.
Next: Write a R program to create the system's idea of the current date with and without time.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-26.php