Day: February 4, 2021

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

Hits: 11 (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 …

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

Hits: 18 (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 …

R Examples for Beginners – R Program to Add Two Vectors

Hits: 6 (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 …

R Examples for Beginners – R “Hello World” Program

Hits: 4 (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 …

R Examples for Beginners – Plotting a uniform distribution in R

Hits: 8 (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 …

R Examples for Beginners – How to filter Data in R

Hits: 4 (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 …

R Examples for Beginners – How read data files in R

Hits: 4 (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 …

R Examples for Beginners – R Lists

Hits: 9 (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” …

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

Hits: 9 (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 != …

R Examples for Beginners – R User Input

Hits: 2 (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 …

R Examples for Beginners – R Loop

Hits: 5 (R Example for Citizen Data Scientist & Business Analyst) countdown <- function(from) { print(from) while(from!=0) { Sys.sleep(1) from <- from – 1 print(from) } } countdown(5) > countdown(5) [1] 5 [1] 4 [1] 3 [1] 2 [1] 1 [1] 0 While loops Use while(condition){code}. Logical operators You can use the usual C-style operators: ==,<,||,&&,…. However, …