w3resource

R Programming: Create and Display Dataframe with Employee details

R Programming: Basic Exercise-25 with Solution

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

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 details of the employees
print("Details of the employees:")

# Print the entire 'Employees' data frame
print(Employees)

Output:

[1] "Details of the employees:"
          Name Gender Age Designation         SSN
1  Anastasia S      M  23       Clerk 123-34-2346
2       Dima R      M  22     Manager  123-44-779
3  Katherine S      F  25    Exective  556-24-433
4      JAMES A      F  26         CEO  123-98-987
5 LAURA MARTIN      M  32   ASSISTANT  679-77-576                         

Explanation:

  • Create Dataframe:
    • Employees = data.frame(...) creates 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") lists the gender of each employee.
    • Age=c(23,22,25,26,32) provides the ages of the employees.
    • Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT") defines their job titles.
    • SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576") includes their Social Security Numbers (SSNs).
  • Display Message and Dataframe:
    • print("Details of the employees:") prints a message indicating that the following output is related to employee details.
    • print(Employees) displays the content of the Employees dataframe.

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 list of heterogeneous data, which include character, numeric and logical vectors. Print the lists.
Next: Write a R program to create a Data Frames which contain details of 5 employees and display summary of the data.

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-25.php