w3resource

Create and Display the Structure of a Data Frame in R Programming

R Programming: Data frame Exercise-3 with Solution

Write a R program to get the structure of a given data frame.

Sample Solution :

R Programming Code :

# Create a data frame named 'exam_data' with four columns: 'name', 'score', 'attempts', and 'qualify'
exam_data = data.frame(
  # Define the 'name' column with a character vector of names
  name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
  
  # Define the 'score' column with a numeric vector of scores
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
  
  # Define the 'attempts' column with an integer vector of attempts
  attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
  
  # Define the 'qualify' column with a character vector of qualification status
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)

# Print the label "Original dataframe:"
print("Original dataframe:")

# Print the data frame 'exam_data'
print(exam_data)

# Print the label "Structure of the said data frame:"
print("Structure of the said data frame:")

# Display the structure of the data frame 'exam_data'
print(str(exam_data))

Output:

[1] "Original dataframe:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0        3      no
3  Katherine  16.5        2     yes
4      James  12.0        3      no
5      Emily   9.0        2      no
6    Michael  20.0        3     yes
7    Matthew  14.5        1     yes
8      Laura  13.5        1      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes
[1] "Structure of the said data frame:"
'data.frame':	10 obs. of  4 variables:
 $ name    : chr  "Anastasia" "Dima" "Katherine" "James" ...
 $ score   : num  12.5 9 16.5 12 9 20 14.5 13.5 8 19
 $ attempts: num  1 3 2 3 2 3 1 1 2 1
 $ qualify : chr  "yes" "no" "yes" "no" ...
NULL             

Explanation:

  • Create a data frame named exam_data:
    Uses the data.frame() function to create a data frame with four columns: name, score, attempts, and qualify.
  • Define the name column:
    A character vector with names: 'Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'.
  • Define the score column:
    A numeric vector with scores: 12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19.
  • Define the attempts column:
    An integer vector indicating the number of attempts: 1, 3, 2, 3, 2, 3, 1, 1, 2, 1.
  • Define the qualify column:
    A character vector indicating qualification status: 'yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes'.
  • Print the label "Original dataframe:":
    Displays the text "Original dataframe:" to indicate the start of the data frame output.
  • Print the data frame exam_data:
    Displays the entire data frame created, showing all rows and columns.
  • Print the label "Structure of the said data frame:":
    Displays the text "Structure of the said data frame:" to indicate the structure output.
  • Display the structure of the data frame:
    Uses the str() function to display the internal structure of the data frame, including data types and column contents.

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 frame from four given vectors.
Next: Write a R program to get the statistical summary and nature of the data of a given data frame.

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/dataframe/r-programming-data-frame-exercise-3.php