R Programming Basics and Examples
R Programming Basics and Examples
Data frames in R are crucial for handling and analyzing structured data. They store data in a tabular format, allowing for advanced manipulation and operations across rows and columns. Data frames support operations like subsetting, aggregation, and are used extensively in statistical analysis and creating complex modeling workflows, enhancing data analysis capabilities .
Loops, such as 'for' or 'while', are used in R for iterating through a sequence of values to perform operations like summation. For example, a 'for' loop like `for (i in 1:50) {total_sum <- total_sum + i}` sums numbers from 1 to 50 incrementally, adding each element to a cumulative total .
In R, a pie chart represents parts of a whole as slices of a circle, using the `pie()` function. It is significant for visualizing data to quickly display proportionate distribution among categories. For example, `pie(market_share, labels = company_names)` visually distinguishes market shares among different brands .
The EMI (Equated Monthly Installment) calculation in R involves computing the total amount repayable per month over the loan tenure. Using the formula `(loan_amount + (loan_amount * annual_interest_rate / 100)) / loan_months`, it offers insights into regular financial commitments required for loan repayment, aiding in financial planning and forecasting .
In R, you apply a service charge to each element of a vector by using vector arithmetic. Adding a fixed amount like ₹20 to each bill is performed by the expression `final_bills <- restaurant_bills + 20`, where `restaurant_bills` is a vector of individual bill amounts .
To calculate the average and percentage of a student's marks in R, obtain the total marks by summing individual scores and dividing by the number of subjects for the average. Calculate the percentage by dividing the total score by the maximum possible score, then multiply by 100. Example code is: total <- m1+m2+m3+m4; perc <- (total/400)*100; averg <- total/4 .
The paste() function in R concatenates multiple strings or variables with a default separator, which is a space. It can merge variables or text to form a single continuous output, such as in the example `paste(a, b)` where `a <- "Data"` and `b <- "Science"` results in "Data Science" .
R employs conditional statements to categorize temperature as 'High', 'Moderate', or 'Low', based on defined thresholds. The use of `if`, `else if`, and `else` statements exemplifies decision-making processes by executing different code paths based on conditions, aiding in immediate categorization and interpretation of temperature data for quick decision-making .
R determines whether a number is even or odd using the modulus operator (%%). For instance, the expression `number %% 2 == 0` checks if the number is evenly divisible by 2, identifying it as even; if not, it is odd .
To calculate and display the GST of a product in R, calculate the GST amount and add it to the original price using arithmetic. The operation `gst <- price + (price * 0.18)` adds an 18% GST to the original price, and `paste("GST : ",gst)` is used to display the result .