Home / Programs / What's that data type in R?
🚀 Programming Example

What's that data type in R?

👁 406 Views
💻 Practical Program
📘 Step Learning

Do you remember that when you added 5 + "six", you got an error due to a mismatch in data types? You can avoid such embarrassing situations by checking the data type of a variable beforehand. You can do this with the class() function, as the code on the right shows.

💻 Program Code

# Declare variables of different types
my_numeric <- 42
my_character <- "universe"
my_logical <- FALSE 

# Check class of my_numeric
class(my_numeric)

# Check class of my_character
class(my_character)

# Check class of my_logical
class(my_logical)

 

                        

🖥 Program Output

> # Declare variables of different types
> my_numeric <- 42
> my_character <- "universe"
> my_logical <- FALSE
> 
> # Check class of my_numeric
> class(my_numeric)
[1] "numeric"
> 
> # Check class of my_character
> class(my_character)
[1] "character"
> 
> # Check class of my_logical
> class(my_logical)
[1] "logical"
                            

📘 Explanation

Complete the code in the editor and also print out the classes of my_character and my_logical.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.