############################ R CODE FOR POWERBI + R WORSKHOP ############################## ############### EXERCISE 1 - OPTION 1 #Following workshop examples will use this dataset data() #show a list of available datasets mydataframe <- trees #copy the tree dataset to mydataframe mytreetype <- sample (c("A","B","C"),nrow(trees),replace=TRUE) mytreelocation <- sample(c("mountain","meadow"),nrow(trees),replace=TRUE) mytreeID <- c(1:nrow(trees)) mydataframe$type <- mytreetype mydataframe$location <- mytreelocation mydataframe$ID <- mytreeID ############## EXERCISE 1 - OPTION 2 getwd()#tells you what your path currently is #you will need to determine the correct path for your data location. path <- "C:/Users/User/Desktop/PowerBI_demo_files/CityTables" #NOTE:forward slashes setwd(path) filename <- "myfilename.csv" mydataframe <- read.csv(filename) #read in file as a dataframe head(mydataframe) #print the first few lines of the dataframe ############### EXERCISE 1 - OPTION 3 getwd() #to make sure at least one command is working customer <- c("Jen","Steve","Deepthi","Gilles") purchaseamount <- c(3100, 3340, 5800,45) purchasetype <- c("fun","practical","fun","both") purchasedate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2012-01-01')) customer_data <- data.frame(customer, purchaseamount, purchasedate,purchasetype) customer_data$purchasetype <- as.character(customer_data$purchasetype) mydataframe <- customer_data ############### EXERCISE 3 - PART 1 #this code assumes you have loaded the modified trees dataset #from Exercise 1 Option 1 into the dataset variable #if you are using a different dataset, you will need to change variable names plot(dataset$Girth)#single variable simple visualization #try with both a categorical variable and numeric variable if your dataset has a categorical variable ############### EXERCISE 3 - PART 2 #this code assumes you have loaded the modified trees dataset #from Exercise 1 Option 1 into the dataset variable #if you are using a different dataset, you will need to change variable names plot(dataset$Girth, dataset$Height)#two variable simple visualization #try with a categorical variable and numeric variable if your dataset has these variables ############### EXERCISE 5 - PART 1 dataset$season <- sample(c("summer","winter"),nrow(trees),replace=TRUE) output <- dataset #if you don't have this, you will overwrite the data with an empty dataset ############### EXERCISE 5 - PART 2 #This is going to replace your existing data with new data customer <- c("Jen","Steve","Deepthi","Gilles") purchaseamount <- c(3100, 3340, 5800,45) purchasetype <- c("fun","practical","fun","both") purchasedate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2012-01-01')) output <- data.frame(customer, purchaseamount, purchasedate,purchasetype) ############### WIDE SAMPLE DATA customer <- c("Jen","Steve","Deepthi","Gilles") purchasedate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14','2012-01-01')) purchaseamount.fun <- c(3100,567,5800,1234) purchaseamount.practical <-c(4800,3340,2800,1000) purchaseamount.both <- c(72,18,67,45) wide_customer_data <- data.frame(customer, purchasedate, purchaseamount.fun,purchaseamount.practical,purchaseamount.both)