Day: February 4, 2021

R Examples for Beginners – R Program to Take Input From User

(R Example for Citizen Data Scientist & Business Analyst)   R Program to Take Input From User In this example, you’ll learn to take input from a user using readline() function. When we are working with R in an interactive session, we can use readline() function to take input from the user (terminal). This function will return …

R Examples for Beginners – Find Sum, Mean and Product of Vector in R Programming

(R Example for Citizen Data Scientist & Business Analyst)   Find Sum, Mean and Product of Vector in R Programming In this example, you will learn to find sum, mean and product of vector elements using built-in functions. We can sum the elements of a vector using the sum() function. Similarly, mean() and prod() functions can be used to find …

R Examples for Beginners – R Program to Add Two Vectors

(R Example for Citizen Data Scientist & Business Analyst)   R Program to Add Two Vectors In this example, you’ll learn to add two vectors using R operators. We can add two vectors together using the + operator. One thing to keep in mind while adding (or other arithmetic operations) two vectors together is the recycling rule. …

R Examples for Beginners – R “Hello World” Program

(R Example for Citizen Data Scientist & Business Analyst)   R “Hello World” Program A simple program to display “Hello World!” on the screen using print() function. Example: Hello World Program > # We can use the print() function > print(“Hello World!”) [1] “Hello World!” > # Quotes can be suppressed in the output > …

R Examples for Beginners – Plotting a uniform distribution in R

(R Example for Citizen Data Scientist & Business Analyst)   n <- floor(runif(1000)*10) t <- table(n) barplot(t)   Overview The first line generates a list of random numbers. The table function counts the number of occurences for each generated number. barplot creates a bar chart based on the table data. Getting the random numbers runif will generate 1000 uniformly distributed …

R Examples for Beginners – How to filter Data in R

(R Example for Citizen Data Scientist & Business Analyst)   This code uses a dataset file with population estimates by the US Census Bureau (more info). tbl <- read.table(file.choose(),header=TRUE,sep=’,’) population <- tbl[c(“NAME”,”POPESTIMATE2009″,”NPOPCHG_2009″)] smallest.state.pop <- min(population$POPESTIMATE2009) print(population[population$POPESTIMATE2009==smallest.state.pop,]) NAME POPESTIMATE2009 NPOPCHG_2009 56 Wyoming 544270 11289 This piece of code extracts the data about the smallest state from the data frame. …

R Examples for Beginners – How read data files in R

(R Example for Citizen Data Scientist & Business Analyst)   This code uses a dataset file with population estimates by the US Census Bureau (more info). tbl <- read.table(file.choose(),header=TRUE,sep=”,”) population <- tbl[“POPESTIMATE2009”] print(summary(population[-1:-5,])) Min. 1st Qu. Median Mean 3rd Qu. Max. 544300 1734000 4141000 5980000 6613000 36960000 Reading a CSV file read.table can read a variety of basic data …

R Examples for Beginners – R Lists

(R Example for Citizen Data Scientist & Business Analyst)   sum(0:9) append(LETTERS[1:13],letters[14:26]) c(1,6,4,9)*2 something <- c(1,4,letters[2]) # indices start at one, you get (1,4,”b”) length(something) sum(0:9) [1] 45 > append(LETTERS[1:13],letters[14:26]) [1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J” “K” “L” “M” “n” “o” [16] “p” “q” “r” “s” “t” “u” “v” “w” …

R Examples for Beginners – Guess a random number game in R

(R Example for Citizen Data Scientist & Business Analyst)   #utility functions readinteger <- function() { n <- readline(prompt=”Enter an integer: “) if(!grepl(“^[0-9]+$”,n)) { return(readinteger()) } return(as.integer(n)) } # real program start here num <- round(runif(1) * 100, digits = 0) guess <- -1 cat(“Guess a number between 0 and 100.n”) while(guess != num) { …

R Examples for Beginners – R User Input

(R Example for Citizen Data Scientist & Business Analyst)   readinteger <- function() { n <- readline(prompt=”Enter an integer: “) return(as.integer(n)) } print(readinteger()) Enter an integer: 88 [1] 88 The readline function readline() lets the user enter a one-line string at the terminal. The prompt argument is printed in front of the user input. It usually ends on …