Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

R Variables


Certainly! Variables in R are like containers that hold data. Here's a simple explanation with examples:

What are Variables?

Variables are names that represent values or data in your R code. You can store different types of data in variables, such as numbers, text, or logical values.

Example:

Example

# Assigning values to variables
x <- 10 # x is a variable holding the value 10
name <- "John" # name is a variable holding the text "John"
is_valid <- TRUE # is_valid is a variable holding the logical value TRUE

Rules for Naming Variables:

Example:

Example

# Valid variable names
age <- 30
first_name <- "Alice"
is_active <- TRUE

# Invalid variable names
3students <- 20 # Variable name cannot start with a number
last-name <- "Smith" # Hyphens are not allowed in variable names

Accessing Variables:

You can use variables in your code to perform operations or access their values.

Example

# Using variables in operations
x <- 5
y <- 3
sum <- x + y # sum will hold the value 8

# Accessing variable values
cat("The sum is:", sum)

Updating Variables:

You can change the value stored in a variable by assigning a new value to it.

Example

# Updating the value of a variable
x <- 10
x <- x + 5 # Now, x will hold the value 15

Variables are fundamental building blocks in R, allowing you to store and manipulate data in your code.


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java