[1] 10
[1] 6
Consists inbuilt functions like sum()
, length()
, sqrt()
,mean()
, summary()
, View()
The sum()
function calculates the total sum of all the elements in a numeric vector.
The length()
function returns the number of elements in a vector (or other objects).
The sqrt()
function calculates the square root of each element in a numeric vector.
The mean()
function calculates the arithmetic mean (average) of the elements in a numeric vector.
The summary()
function in R provides a concise statistical summary of objects like vectors, matrices, data frames, and results of model fitting.
data.frame()
function is used to create data frames, which are table-like structures consisting of rows and columns. - Data frames are one of the most important data structures in R, especially for statistical modeling and data analysis.
creating data frame
# Creating a simple data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Height = c(5.5, 6.0, 5.8)
)
df
Name Age Height
1 Alice 25 5.5
2 Bob 30 6.0
3 Charlie 35 5.8
View() function
is used to invoke a spreadsheet-like data viewer on a data frame, matrix, or other objects that can be coerced into a data frame. - This function is particularly useful during interactive sessions to inspect data visually.
Name Age Height
1 Alice 25 5.5
2 Bob 30 6.0
3 Charlie 35 5.8
Use for
, while
.
The for loop in R is used to iterate over a sequence (like a vector or a list) and execute a block of code for each element in the sequence.
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
The while loop executes a block of code as long as the specified condition is TRUE
while loop
# Print numbers from 1 to 5 using while
i <- 1 # Initialize counter
while(i <= 4) {
print(i)
i <- i + 1 # Increment counte
}
[1] 1
[1] 2
[1] 3
[1] 4