# clean data in R (not programmatically) setwd('S:/Courses/stat-renaes/Stat404') nonsales=read.csv("nonsales.csv",header=T) fix(nonsales) rm(nonsales) # because we fixed it and I want to "fix" it again # clean data programmatically nonsales=read.csv("nonsales.csv",header=T) attach(nonsales) # Need country variable to be US or AU (not us or au) nonsales$Country=toupper(Country) # Use package DataCombine (and yes there are other merging methods in this we will # look at) install.packages('DataCombine') library('DataCombine') # FindReplace # this command will replace values (categorical) # the easiest way to use this is to create vectors of new names # use from and to in the new names ABData <- data.frame(a = c("London, UK", "Oxford, UK", "Berlin, DE", "Hamburg, DE", "Oslo, NO"), b = c(8, 0.1, 3, 2, 1)) Replaces <- data.frame(from = c("UK", "DE"), to = c("England", "Germany")) install.package("DataCombine") library(DataCombine) ABNewDF <- FindReplace(data = ABData, Var = "a", replaceData = Replaces, from = "from", to = "to", exact = FALSE) # conditional # if( ) x=5 if(x<0){ print('Positive number') } # if else x=-5 if(x > 0){ print("Non-negative number") } else { print("Negative number") } # nested if else x <- 0 if (x < 0) { print("Negative number") } else if (x > 0) { print("Positive number") } else print("Zero") # ifelse( ) x=2 ifelse(x>0,print("Positive number"),print("Negative number"))