Presented by : Deepak Sharma
[ Use arrow keys to go to prev/next page ]
All styles have same results, one is better other is best of all
a <- 10
b <- 20
c <- (10+20)/2
c
[1] 15
All styles have same results, one is better other is best of all
variable_number_1 <- 10
variable_number_2 <- 20
third_variable <- mean(c(variable_number_1,variable_number_2))
print(third_variable)
[1] 15
All styles have same results, one is better other is best of all
# Add comments
# Indent if it improves readability
var1 <- 10 # First variable
var2 <- 20 # Second variable
# Calculate average value using standard mean() function
average_value <- mean(c(var1,var2))
# Display the average value properly
cat('Mean value is : ',average_value)
Mean value is : 15
# Install a package everytime you run a script
install.packages("nnet")
# Import your package
library(nnet)
# Writing a robust code - a nice way to check & install a package
load_pkgs <- c("e1071","caret")
# Perform a for loop for each item in pkg list
for (i in load_pkgs)
{
if(! (i %in% installed.packages())) # If package is not installed
install.packages(i) # Install the package
do.call('library',as.list(i)) # Load the library if already installed
}
# To know the version of R which you are using
version
# Automatically update your r version with the package installr
library(installr)
# Use updateR() function to update R
updateR()
numbers <- 1:10000000
system.time({ # R is case sensitive ('System' is not equals to 'system')
total <- 0
for(i in numbers) # Iterative approach
total <- total + i
cat("Sum is : ",total,"\n")
})
Sum is : 5e+13
user system elapsed
0.35 0.00 0.35
# Try writing efficient programs (Save time)
numbers <- 1:10000000
system.time({
total <- 0
total <- sum(c(numbers)) # Vectorized code
cat("Sum is : ",total,"\n")
})
Sum is : 5e+13
user system elapsed
0.13 0.00 0.12
# Ctrl + Shift + C # Comment selected lines
# Ctrl + Shift + R # Insert section
# Ctrl + L # Clear console
rm(list=ls()) # Clear all workspace variables
# Use an IDE such as RStudio
cat('\f') # To clear your console
# Press [`Alt` + `-`] to put <- or `assignment operator` in RStudio